一个简单例子初步了解主键、索引、sql优化的重要性

测试数据:条数100W,查询目标数据只有一条,选取的数据为所有数据的中间数据,为了公正。在进行操作时没有增加进程

初学者可以纵向、横向比较
一、未加主键、未加索引

select * from myindex where username like ‘username500000%’
456 458 456 454 464 466 456 457 460 458 平均458ms
select * from myindex where username like ‘%username500000%’
547 549 554 551 547 550 554 556 553 547 平均550ms
select * from myindex where username =‘username500000’
408 413 415 406 400 411 406 407 406 410 平均408ms
select * from myindex where username in (‘username500000’)
409 406 406 410 406 408 412 414 410 402 平均408ms
二、加主键

select * from myindex where username like ‘username500000%’
358 333 333 337 335 332 331 331 336 334 平均337ms
select * from myindex where username like ‘%username500000%’
418 419 423 420 424 419 421 417 417 418 平均420ms
select * from myindex where username =‘username500000’
283 284 283 282 285 283 284 283 285 284 平均283ms
select * from myindex where username in (‘username500000’)
281 285 295 288 285 283 285 291 286 299 平均285ms
三、加主键+目标列加索Unique—btree

select * from myindex where username like ‘username500000%’
2 1 1 0 1 1 1 1 0 1 平均1ms
select * from myindex where username like ‘%username500000%’
420 426 416 417 415 418 418 416 419 418 平均420ms
select * from myindex where username =‘username500000’
1 1 1 0 1 1 1 1 1 1 平均1ms
select * from myindex where username in (‘username500000’)
0 1 1 0 0 0 1 0 1 0 平均1ms
四、目标列加索Unique—btree
select * from myindex where username like ‘username500000%’
1 0 0 1 1 0 1 0 1 0 平均1ms
select * from myindex where username like ‘%username500000%’
546 542 543 544 543 546 551 557 560 547 平均546ms
select * from myindex where username =‘username500000’
0 1 1 2 1 1 1 1 1 0 平均1ms
select * from myindex where username in (‘username500000’)
1 0 1 0 0 1 1 1 0 1 平均1ms

初学者可以在网上找找sql优化策略、以及索引的添加

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值