mysql中的预编译语句

在oracle中,都是强制开发使用绑定变量的,如果不使用绑定变量,那么系统没有扩展性,并发稍微多些后,cpu基本就满了,但是在mysql中还没有看到过说一定要使用绑定变量的说法,即使有使用绑定变量的说法也是从安全的角度去建议使用绑定变量。下面测试一下mysql中使用绑定变量和不使用绑定变量的条件下,对于cpu的消耗有哪些区别。
测试数据如下,在测试的过程中,100线程情况下,使用和不适用预编译消耗的cpu是差不多的,执行时间上,使用预编译的情况会好些,但是提升的效果不是很明显,可能是我的测试用例比较简单只有单个语句,3%的提升。
在500用户的测试情况下,cpu有接近10%的减少。时间上也是有3%的提升。

使用Statment耗时:138337
donw
使用Statment耗时:138872
donw
使用Statment耗时:139150
donw
使用Statment耗时:139139
donw
使用Statment耗时:139357
donw
使用Statment耗时:139369
donw
使用Statment耗时:139422
donw
使用Statment耗时:139460
donw
使用Statment耗时:139442
donw
使用Statment耗时:139529
donw
使用Statment耗时:139618
donw
使用Statment耗时:139698
donw
使用Statment耗时:139980
donw
使用Statment耗时:140000
donw
使用Statment耗时:140262
donw
使用Statment耗时:140316
donw
使用Statment耗时:140358
donw
使用Statment耗时:140359
donw
使用Statment耗时:140363
donw
使用Statment耗时:140362
donw
使用Statment耗时:140382
donw
使用Statment耗时:140407
donw
使用Statment耗时:140436
donw
使用Statment耗时:140475
donw
使用Statment耗时:140577
donw
使用Statment耗时:140599
donw
使用Statment耗时:140621
donw
使用Statment耗时:140612
donw
使用Statment耗时:140627
donw
使用Statment耗时:140676
donw
使用Statment耗时:140689
donw
使用Statment耗时:140678
donw
使用Statment耗时:140718
donw
使用Statment耗时:140717
donw
使用Statment耗时:140705
donw
使用Statment耗时:140847
donw
使用Statment耗时:140838
donw
使用Statment耗时:140847
donw
使用Statment耗时:140877
donw
使用Statment耗时:140858
donw
使用Statment耗时:140858
donw
使用Statment耗时:140944
donw
使用Statment耗时:140966
donw
使用Statment耗时:140948
donw
使用Statment耗时:140947
donw
使用Statment耗时:140975
donw
使用Statment耗时:140965
donw
使用Statment耗时:141001
donw
使用Statment耗时:141017
donw
使用Statment耗时:141037
donw
使用Statment耗时:141052
donw
使用Statment耗时:141068
donw
使用Statment耗时:141113
donw
使用Statment耗时:141178
donw
使用Statment耗时:141164
donw
使用Statment耗时:141186
donw
使用Statment耗时:141207
donw
使用Statment耗时:141264
donw
使用Statment耗时:141303
donw
使用Statment耗时:141380
donw
使用Statment耗时:141376
donw
使用Statment耗时:141391
donw
使用Statment耗时:141419
donw
使用Statment耗时:141392
donw
使用Statment耗时:141467
donw
使用Statment耗时:141515
donw
使用Statment耗时:141501
donw
使用Statment耗时:141517
donw
使用Statment耗时:141503
donw
使用Statment耗时:141522
donw
使用Statment耗时:141538
donw
使用Statment耗时:141639
donw
使用Statment耗时:141643
donw
使用Statment耗时:141662
donw
使用Statment耗时:141668
donw
使用Statment耗时:141754
donw
使用Statment耗时:141761
donw
使用Statment耗时:141782
donw
使用Statment耗时:141786
donw
使用Statment耗时:141789
donw
使用Statment耗时:141844
donw
使用Statment耗时:141853
donw
使用Statment耗时:141877
donw
使用Statment耗时:141889
donw
使用Statment耗时:141902
donw
使用Statment耗时:141997
donw
使用Statment耗时:142026
donw
使用Statment耗时:142190
donw
使用Statment耗时:142254
donw
使用Statment耗时:142280
donw
使用Statment耗时:142291
donw
使用Statment耗时:142380
donw
使用Statment耗时:142359
donw
使用Statment耗时:142470
donw
使用Statment耗时:142556
donw
使用Statment耗时:142562
donw
使用Statment耗时:142637
donw
使用Statment耗时:142714
donw
使用Statment耗时:142727
donw
使用Statment耗时:143405

使用预编译的情况

成功加载MySQL驱动程序
使用prestment耗时:137333
donw
使用prestment耗时:137913
donw
使用prestment耗时:137943
donw
使用prestment耗时:137937
donw
使用prestment耗时:138191
donw
使用prestment耗时:138336
donw
使用prestment耗时:138362
donw
使用prestment耗时:138374
donw
使用prestment耗时:138411
donw
使用prestment耗时:138703
donw
使用prestment耗时:138889
donw
使用prestment耗时:138966
donw
使用prestment耗时:138972
donw
使用prestment耗时:138978
donw
使用prestment耗时:138961
donw
使用prestment耗时:138985
donw
使用prestment耗时:138980
donw
使用prestment耗时:139057
donw
使用prestment耗时:139040
donw
使用prestment耗时:139118
donw
使用prestment耗时:139153
donw
使用prestment耗时:139210
donw
使用prestment耗时:139228
donw
使用prestment耗时:139226
donw
使用prestment耗时:139309
donw
使用prestment耗时:139317
donw
使用prestment耗时:139431
donw
使用prestment耗时:139434
donw
使用prestment耗时:139558
donw
使用prestment耗时:139562
donw
使用prestment耗时:139556
donw
使用prestment耗时:139659
donw
使用prestment耗时:139661
donw
使用prestment耗时:139654
donw
使用prestment耗时:139726
donw
使用prestment耗时:139771
donw
使用prestment耗时:139826
donw
使用prestment耗时:139832
donw
使用prestment耗时:139850
donw
使用prestment耗时:139831
donw
使用prestment耗时:139876
donw
使用prestment耗时:139905
donw
使用prestment耗时:139914
donw
使用prestment耗时:139905
donw
使用prestment耗时:139916
donw
使用prestment耗时:139932
donw
使用prestment耗时:139929
donw
使用prestment耗时:139979
donw
使用prestment耗时:139991
donw
使用prestment耗时:140011
donw
使用prestment耗时:140007
donw
使用prestment耗时:140018
donw
使用prestment耗时:140111
donw
使用prestment耗时:140102
donw
使用prestment耗时:140108
donw
使用prestment耗时:140106
donw
使用prestment耗时:140102
donw
使用prestment耗时:140108
donw
使用prestment耗时:140119
donw
使用prestment耗时:140114
donw
使用prestment耗时:140119
donw
使用prestment耗时:140214
donw
使用prestment耗时:140236
donw
使用prestment耗时:140241
donw
使用prestment耗时:140213
donw
使用prestment耗时:140300
donw
使用prestment耗时:140300
donw
使用prestment耗时:140293
donw
使用prestment耗时:140305
donw
使用prestment耗时:140305
donw
使用prestment耗时:140301
donw
使用prestment耗时:140323
donw
使用prestment耗时:140318
donw
使用prestment耗时:140321
donw
使用prestment耗时:140403
donw
使用prestment耗时:140407
donw
使用prestment耗时:140409
donw
使用prestment耗时:140437
donw
使用prestment耗时:140452
donw
使用prestment耗时:140457
donw
使用prestment耗时:140501
donw
使用prestment耗时:140580
donw
使用prestment耗时:140647
donw
使用prestment耗时:140688
donw
使用prestment耗时:140718
donw
使用prestment耗时:140729
donw
使用prestment耗时:140735
donw
使用prestment耗时:140734
donw
使用prestment耗时:140904
donw
使用prestment耗时:140976
donw
使用prestment耗时:140970
donw
使用prestment耗时:141106
donw
使用prestment耗时:141183
donw
使用prestment耗时:141245
donw
使用prestment耗时:141255
donw
使用prestment耗时:141338
donw
使用prestment耗时:141414
donw
使用prestment耗时:141414
donw
使用prestment耗时:141866
donw
使用prestment耗时:141982

500并发的情况
无预编译的情况
cpu

03:02:14 PM 1893 5358 69.15 12.44 0.00 81.59 1 mysqld
03:02:16 PM 1893 5358 80.00 7.50 0.00 87.50 1 mysqld
03:02:19 PM 1893 5358 78.50 14.00 0.00 92.50 1 mysqld
03:02:21 PM 1893 5358 69.00 13.00 0.00 82.00 1 mysqld
03:02:23 PM 1893 5358 70.65 15.92 0.00 86.57 1 mysqld
03:02:25 PM 1893 5358 78.11 15.42 0.00 93.53 1 mysqld
03:02:27 PM 1893 5358 80.10 16.92 0.00 97.01 1 mysqld
03:02:29 PM 1893 5358 77.00 14.50 0.00 91.50 1 mysqld
03:02:31 PM 1893 5358 82.50 17.50 0.00 100.00 1 mysqld
03:02:33 PM 1893 5358 84.00 17.50 0.00 100.00 1 mysqld
03:02:35 PM 1893 5358 76.62 12.94 0.00 89.55 1 mysqld
03:02:37 PM 1893 5358 75.50 14.00 0.00 89.50 1 mysqld
03:02:39 PM 1893 5358 76.50 12.50 0.00 89.00 1 mysqld
03:02:41 PM 1893 5358 72.14 12.94 0.00 85.07 1 mysqld
03:02:43 PM 1893 5358 87.56 17.91 0.00 100.00 1 mysqld
03:02:45 PM 1893 5358 79.10 15.92 0.00 95.02 1 mysqld

使用prestment耗时:2549563
donw
使用prestment耗时:2549722
donw
使用prestment耗时:2550565
donw
使用prestment耗时:2550393
donw
使用prestment耗时:2550779
donw
使用prestment耗时:2551235
donw
使用prestment耗时:2551603
donw
使用prestment耗时:2552023
donw
使用prestment耗时:2552113
donw
使用prestment耗时:2552963
donw
使用prestment耗时:2554546
donw
使用prestment耗时:2554891
donw
使用prestment耗时:2556299
donw
使用prestment耗时:2556401
donw
使用prestment耗时:2559027
donw
使用prestment耗时:2559495
donw
使用prestment耗时:2562710
donw
使用prestment耗时:2574838

预编译的情况
cpu
3:46:01 PM UID PID %usr %system %guest %CPU CPU Command
03:46:03 PM 1893 5358 59.20 15.42 0.00 74.63 1 mysqld
03:46:05 PM 1893 5358 64.68 16.42 0.00 81.09 1 mysqld
03:46:07 PM 1893 5358 34.00 12.00 0.00 46.00 1 mysqld
03:46:09 PM 1893 5358 64.00 14.00 0.00 78.00 1 mysqld
03:46:11 PM 1893 5358 53.23 12.44 0.00 65.67 1 mysqld
03:46:13 PM 1893 5358 56.00 15.50 0.00 71.50 1 mysqld
03:46:15 PM 1893 5358 61.69 15.92 0.00 77.61 1 mysqld
03:46:17 PM 1893 5358 58.00 14.50 0.00 72.50 1 mysqld
03:46:19 PM 1893 5358 59.20 15.42 0.00 74.63 1 mysqld
03:46:21 PM 1893 5358 63.18 13.93 0.00 77.11 1 mysqld
03:46:23 PM 1893 5358 61.00 17.00 0.00 78.00 1 mysqld
03:46:25 PM 1893 5358 66.67 15.92 0.00 82.59 1 mysqld
03:46:27 PM 1893 5358 58.50 15.50 0.00 74.00 1 mysqld
03:46:29 PM 1893 5358 69.00 14.50 0.00 83.50 1 mysqld
03:46:31 PM 1893 5358 57.50 14.00 0.00 71.50 1 mysqld
03:46:33 PM 1893 5358 62.19 15.92 0.00 78.11 1 mysqld
03:46:35 PM 1893 5358 61.19 14.93 0.00 76.12 1 mysq

使用prestment耗时:2490864
donw
使用prestment耗时:2491155
donw
使用prestment耗时:2491443
donw
使用prestment耗时:2491444
donw
使用prestment耗时:2491969
donw
使用prestment耗时:2492106
donw
使用prestment耗时:2492403
donw
使用prestment耗时:2493323
donw
使用prestment耗时:2493960
donw
使用prestment耗时:2494412
donw
使用prestment耗时:2495219
donw
使用prestment耗时:2495254
donw
使用prestment耗时:2496365
donw
使用prestment耗时:2496218
donw
  conn = DriverManager.getConnection(conn_str);
            PreparedStatement pstmt = conn.prepareStatement("insert into bai(name) values( ?)");

            for (int i = 0; i < 10000; i++) {
                pstmt.setString(1,String.valueOf(i));
                pstmt.executeUpdate();
                //  ResultSet result = stmt.executeUpdate(sql);
            }

MySQL 5.6提供了对服务器端准备语句的支持。 此支持利用高效的客户端/服务器二进制协议。 使用带有占位符的准备好的语句来获取参数值具有以下好处:

每次执行时都不用分析语句的开销。 通常情况下,数据库应用程序处理大量几乎完全相同的语句,只对WHERE查询和删除,SET更新和VALUES等子句中的文字或变量值进行更改。

防止SQL注入攻击。 参数值可以包含未转义的SQL引号和分隔符字符。
mysql预编译相关的参数
max_prepared_stmt_count
该变量限制了服务器中预备语句的总数。 (所有会话中已准备好的语句总数。)它可用于有可能发生拒绝服务攻击的环境,这种攻击是基于通过准备大量语句来运行服务器而造成的。 如果该值设置为低于当前准备好的语句数量,则现有语句不受影响并且可以使用,但是直到当前数字低于限制时才能准备新的语句。 默认值是16,382。 允许的值范围是从0到100万。 将该值设置为0会禁用预准备语句。

状态变量show global status like ‘Prepared%’;显示的是当前的预编译语句数量,连接执行完后,会被清0
jdbc中跟预编译相关的参数设置
useServerPrepStmts=true&cachePrepStmts=true&prepStmtCacheSize=256&prepStmtCacheSqlLimit=256

这个参数如果不设置跟没有使用预编译的效果是一样的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值