sql 效率

本文通过对比不同条件组合下的SQL查询语句及其执行计划,分析了MySQL查询效率的影响因素。实验表明,使用IN操作而非复杂的OR逻辑能更好地利用索引,减少文件排序需求,从而提升查询速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. mysql> explain SELECT SQL_CALC_FOUND_ROWS * FROM logs WHERE facility IN ('cron') AND priority IN ('warning','err','crit') AND ( msg like '%jjj%') AND ((program = 'crond' and msg RLIKE '0') or (program='crontab') or (program ='exiting') or (program ='HOSTIDM' AND msg LIKE '%vdfgd34345%' )) ORDER BY seq desc, datetime DESC 
  2.     -> ;
  3. +----+-------------+-------+------+---------------------------+----------+---------+-------+-------+-----------------------------+
  4. | id | select_type | table | type | possible_keys             | key      | key_len | ref   | rows  | Extra                       |
  5. +----+-------------+-------+------+---------------------------+----------+---------+-------+-------+-----------------------------+
  6. |  1 | SIMPLE      | logs  | ref  | program,priority,facility | facility | 33      | const | 84680 | Using where; Using filesort |
  7. +----+-------------+-------+------+---------------------------+----------+---------+-------+-------+-----------------------------+
  8. 1 row in set (0.00 sec)
  9. mysql> explain SELECT SQL_CALC_FOUND_ROWS * FROM logs WHERE facility IN ('cron') AND priority IN ('warning','err','crit') AND ( msg like '%jjj%') AND program IN ('crond','crontab','exiting','HOSTIDM') AND msg RLIKE '0' AND msg LIKE '%vdfgd34345%' ORDER BY seq desc, datetime DESC ;
  10. +----+-------------+-------+------+---------------------------+----------+---------+-------+-------+-----------------------------+
  11. | id | select_type | table | type | possible_keys             | key      | key_len | ref   | rows  | Extra                       |
  12. +----+-------------+-------+------+---------------------------+----------+---------+-------+-------+-----------------------------+
  13. |  1 | SIMPLE      | logs  | ref  | program,priority,facility | facility | 33      | const | 84680 | Using where; Using filesort |
  14. +----+-------------+-------+------+---------------------------+----------+---------+-------+-------+-----------------------------+
  15. 1 row in set (0.00 sec)
  16. mysql> explain SELECT SQL_CALC_FOUND_ROWS * FROM logs WHERE  ( msg like '%jjj%') AND ((program = 'crond' and msg RLIKE '0') or (program='crontab') or (program ='exiting') or (program ='HOSTIDM' AND msg LIKE '%vdfgd34345%' )) ORDER BY seq desc, datetime DESC 
  17.     -> ;
  18. +----+-------------+-------+-------+---------------+---------+---------+------+--------+-----------------------------+
  19. | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows   | Extra                       |
  20. +----+-------------+-------+-------+---------------+---------+---------+------+--------+-----------------------------+
  21. |  1 | SIMPLE      | logs  | range | program       | program | 48      | NULL | 139565 | Using where; Using filesort |
  22. +----+-------------+-------+-------+---------------+---------+---------+------+--------+-----------------------------+
  23. 1 row in set (0.01 sec)
  24. mysql> explain SELECT SQL_CALC_FOUND_ROWS * FROM logs WHERE  ( msg like '%jjj%') AND program IN ('crond','crontab','exiting','HOSTIDM') AND msg RLIKE '0' AND msg LIKE '%vdfgd34345%' ORDER BY seq desc, datetime DESC ;
  25. +----+-------------+-------+-------+---------------+---------+---------+------+--------+-----------------------------+
  26. | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows   | Extra                       |
  27. +----+-------------+-------+-------+---------------+---------+---------+------+--------+-----------------------------+
  28. |  1 | SIMPLE      | logs  | range | program       | program | 48      | NULL | 139565 | Using where; Using filesort |
  29. +----+-------------+-------+-------+---------------+---------+---------+------+--------+-----------------------------+
  30. 1 row in set (0.00 sec)
  31. mysql> SELECT SQL_CALC_FOUND_ROWS * FROM logs WHERE  ( msg like '%jjj%') AND ((program = 'crond' and msg RLIKE '0') or (program='crontab') or (program ='exiting') or (program ='HOSTIDM' AND msg LIKE '%vdfgd34345%' )) ORDER BY seq desc, datetime DESC ;
  32. Empty set (0.62 sec)
  33. mysql> SELECT SQL_CALC_FOUND_ROWS * FROM logs WHERE  ( msg like '%jjj%') AND program IN ('crond','crontab','exiting','HOSTIDM') AND msg RLIKE '0' AND msg LIKE '%vdfgd34345%' ORDER BY seq desc, datetime DESC ;
  34. Empty set (0.53 sec)
  35. mysql> SELECT SQL_CALC_FOUND_ROWS * FROM logs WHERE  ( msg like '%jjj%') AND ((program = 'crond' and msg RLIKE '0') or (program='crontab') or (program ='exiting') or (program ='HOSTIDM' AND msg LIKE '%vdfgd34345%' )) ORDER BY seq desc, datetime DESC ;
  36. Empty set (0.59 sec)
  37. mysql> SELECT SQL_CALC_FOUND_ROWS * FROM logs WHERE  ( msg like '%jjj%') AND program IN ('crond','crontab','exiting','HOSTIDM') AND msg RLIKE '0' AND msg LIKE '%vdfgd34345%' ORDER BY seq desc, datetime DESC ;
  38. Empty set (0.59 sec)
  39. mysql> SELECT SQL_CALC_FOUND_ROWS * FROM logs WHERE  ( msg like '%jjj%') AND ((program = 'crond' and msg RLIKE '0') or (program='crontab') or (program ='exiting') or (program ='HOSTIDM' AND msg LIKE '%vdfgd34345%' )) ORDER BY seq desc, datetime DESC ;
  40. Empty set (0.60 sec)
  41. mysql> SELECT SQL_CALC_FOUND_ROWS * FROM logs WHERE  ( msg like '%jjj%') AND program IN ('crond','crontab','exiting','HOSTIDM') AND msg RLIKE '0' AND msg LIKE '%vdfgd34345%' ORDER BY seq desc, datetime DESC ;
  42. Empty set (0.58 sec)

 

  1. mysql> show index from logs;
  2. +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
  3. | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
  4. +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
  5. | logs  |          0 | PRIMARY  |            1 | seq         | A         |     3225149 |     NULL | NULL   |      | BTREE      |         |
  6. | logs  |          1 | host     |            1 | host        | A         |        1527 |     NULL | NULL   | YES  | BTREE      |         |
  7. | logs  |          1 | program  |            1 | program     | A         |          85 |     NULL | NULL   | YES  | BTREE      |         |
  8. | logs  |          1 | datetime |            1 | datetime    | A         |      806287 |     NULL | NULL   | YES  | BTREE      |         |
  9. | logs  |          1 | priority |            1 | priority    | A         |          85 |     NULL | NULL   | YES  | BTREE      |         |
  10. | logs  |          1 | facility |            1 | facility    | A         |          85 |     NULL | NULL   | YES  | BTREE      |         |
  11. +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
  12. 6 rows in set (0.00 sec)

 

  1. mysql> show index from logs;
  2. +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
  3. | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
  4. +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
  5. | logs  |          0 | PRIMARY  |            1 | seq         | A         |     3225149 |     NULL | NULL   |      | BTREE      |         |
  6. | logs  |          1 | host     |            1 | host        | A         |        1527 |     NULL | NULL   | YES  | BTREE      |         |
  7. | logs  |          1 | program  |            1 | program     | A         |          85 |     NULL | NULL   | YES  | BTREE      |         |
  8. | logs  |          1 | datetime |            1 | datetime    | A         |      806287 |     NULL | NULL   | YES  | BTREE      |         |
  9. | logs  |          1 | priority |            1 | priority    | A         |          85 |     NULL | NULL   | YES  | BTREE      |         |
  10. | logs  |          1 | facility |            1 | facility    | A         |          85 |     NULL | NULL   | YES  | BTREE      |         |
  11. +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
  12. 6 rows in set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值