TARGET_AFFECT_ROWS 与 compound-statement 减少SQL交互的方案

本文介绍了一种MySQL事务处理的新方法,通过在UPDATE和DELETE语句中加入特定HINT来判断影响行数,从而实现在一次交互中完成事务的提交或回滚。

背景

       上篇文章我们介绍了MySQL支持的compound statement SQL的语法。有同学指出这个只是“部分compound”,因为这种多语句语法中不支持if这样的逻辑操作,因此在业务中的使用会受限。

 

       这里我们给出一个常见的事务的逻辑。

    1) 更新一行

   2)若更新成功一行,则在另外一个表中插入一行,否则事务回滚。

  在这样的需求下,就是由于少了逻辑判断,导致无法用compound statement发整个事务,因此需要多次交互。

 

新增HINT

       在这类需求中我们发现,其实我们要的只是一个判断更新是否成功的判断。

因此设计这样的HINT及逻辑:

       UPDATEDELETE语句中增加HINT /*TARGET_AFFECT_ROWS n*/表示这个语句要求修改n行,若不满足这个条件,则认为事务需要回滚。

 

 

使用

       有了这个功能,在实现上诉的业务逻辑时,我们就能直接将以下语句发给Server:

       delimiter ;;

   begin; update /*TARGET_AFFECT_ROWS 1*/ t1  set b=b+1 where a=1 and b>=0; insert into ….; commit;;

      

       这样t1表中a=1这行的b<0, 整个事务无效.

 

小结

       其好处就是

       1\ 在正常执行时,只需要一次交互

       2\ 在需要回滚时,无需再发一个rollback命令。

 

     另外,特别说明,整个方案idea绝大部分来自于 @dbatools

jzuser@vpc87-3:~/Work_dir/Gn/pystudy/NnuNet/nnUNet$ nnUNetv2_plan_and_preprocess -d 3 --verify_dataset_integrity --npfp 8 --npp 8 usage: nnUNetv2_plan_and_preprocess [-h] [-d D [D ...]] [-fpe FPE] [-npfp NPFP] [--verify_dataset_integrity] [--no_pp] [--clean] [-pl PL] [-gpu_memory_target GPU_MEMORY_TARGET] [-preprocessor_name PREPROCESSOR_NAME] [-overwrite_target_spacing OVERWRITE_TARGET_SPACING [OVERWRITE_TARGET_SPACING ...]] [-overwrite_plans_name OVERWRITE_PLANS_NAME] [-c C [C ...]] [-np NP [NP ...]] [--verbose] nnUNetv2_plan_and_preprocess: error: unrecognized arguments: --npfp 8 --npp 8 jzuser@vpc87-3:~/Work_dir/Gn/pystudy/NnuNet/nnUNet$ nnUNetv2_plan_and_preprocess -h usage: nnUNetv2_plan_and_preprocess [-h] [-d D [D ...]] [-fpe FPE] [-npfp NPFP] [--verify_dataset_integrity] [--no_pp] [--clean] [-pl PL] [-gpu_memory_target GPU_MEMORY_TARGET] [-preprocessor_name PREPROCESSOR_NAME] [-overwrite_target_spacing OVERWRITE_TARGET_SPACING [OVERWRITE_TARGET_SPACING ...]] [-overwrite_plans_name OVERWRITE_PLANS_NAME] [-c C [C ...]] [-np NP [NP ...]] [--verbose] options: -h, --help show this help message and exit -d D [D ...] [REQUIRED] List of dataset IDs. Example: 2 4 5. This will run fingerprint extraction, experiment planning and preprocessing for these datasets. Can of course also be just one dataset -fpe FPE [OPTIONAL] Name of the Dataset Fingerprint Extractor class that should be used. Default is 'DatasetFingerprintExtractor'. -npfp NPFP [OPTIONAL] Number of processes used for fingerprint extraction. Default: 8 --verify_dataset_integrity [RECOMMENDED] set this flag to check the dataset integrity. This is useful and should be done once for each dataset! --no_pp [OPTIONAL] Set this to only run fingerprint extraction and experiment planning (no preprocesing). Useful for debugging. --clean [OPTIONAL] Set this flag to overwrite existing fingerprints. If this flag is not set and a fingerprint already exists, the fingerprint extractor will not run. REQUIRED IF YOU CHANGE THE DATASET FINGERPRINT EXTRACTOR OR MAKE CHANGES TO THE DATASET! -pl PL [OPTIONAL] Name of the Experiment Planner class that should be used. Default is 'ExperimentPlanner'. Note: There is no longer a distinction between 2d and 3d planner. It's an all in one solution now. Wuch. Such amazing. -gpu_memory_target GPU_MEMORY_TARGET [OPTIONAL] DANGER ZONE! Sets a custom GPU memory target. Default: 8 [GB]. Changing this will affect patch and batch size and will definitely affect your models performance! Only use this if you really know what you are doing and NEVER use this without running the default nnU-Net first (as a baseline). -preprocessor_name PREPROCESSOR_NAME [OPTIONAL] DANGER ZONE! Sets a custom preprocessor class. This class must be located in nnunetv2.preprocessing. Default: 'DefaultPreprocessor'. Changing this may affect your models performance! Only use this if you really know what you are doing and NEVER use this without running the default nnU-Net first (as a baseline). -overwrite_target_spacing OVERWRITE_TARGET_SPACING [OVERWRITE_TARGET_SPACING ...] [OPTIONAL] DANGER ZONE! Sets a custom target spacing for the 3d_fullres and 3d_cascade_fullres configurations. Default: None [no changes]. Changing this will affect image size and potentially patch and batch size. This will definitely affect your models performance! Only use this if you really know what you are doing and NEVER use this without running the default nnU-Net first (as a baseline). Changing the target spacing for the other configurations is currently not implemented. New target spacing must be a list of three numbers! -overwrite_plans_name OVERWRITE_PLANS_NAME [OPTIONAL] uSE A CUSTOM PLANS IDENTIFIER. If you used -gpu_memory_target, -preprocessor_name or -overwrite_target_spacing it is best practice to use -overwrite_plans_name to generate a differently named plans file such that the nnunet default plans are not overwritten. You will then need to specify your custom plans file with -p whenever running other nnunet commands (training, inference etc) -c C [C ...] [OPTIONAL] Configurations for which the preprocessing should be run. Default: 2d 3f_fullres 3d_lowres. 3d_cascade_fullres does not need to be specified because it uses the data from 3f_fullres. Configurations that do not exist for some dataset will be skipped. -np NP [NP ...] [OPTIONAL] Use this to define how many processes are to be used. If this is just one number then this number of processes is used for all configurations specified with -c. If it's a list of numbers this list must have as many elements as there are configurations. We then iterate over zip(configs, num_processes) to determine then umber of processes used for each configuration. More processes is always faster (up to the number of threads your PC can support, so 8 for a 4 core CPU with hyperthreading. If you don't know what that is then dont touch it, or at least don't increase it!). DANGER: More often than not the number of processes that can be used is limited by the amount of RAM available. Image resampling takes up a lot of RAM. MONITOR RAM USAGE AND DECREASE -np IF YOUR RAM FILLS UP TOO MUCH!. Default: 8 4 8 (=8 processes for 2d, 4 for 3d_fullres and 8 for 3d_lowres if -c is at its default) --verbose Set this to print a lot of stuff. Useful for debugging. Will disable progrewss bar! Recommended for cluster environments 命令还是不对
最新发布
08-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值