当前位置:网站首页>Use index to optimize SQL query "suggestions collection"
Use index to optimize SQL query "suggestions collection"
2022-07-26 08:40:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
Have a look first arena_match_index The table structure , Pay attention to the index structure of the table
CREATE TABLE `arena_match_index` (
`tid` int(10) unsigned NOT NULL DEFAULT '0',
`mid` int(10) unsigned NOT NULL DEFAULT '0',
`group` int(10) unsigned NOT NULL DEFAULT '0',
`round` tinyint(3) unsigned NOT NULL DEFAULT '0',
`day` date NOT NULL DEFAULT '0000-00-00',
`begintime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
UNIQUE KEY `tm` (`tid`,`mid`),
KEY `mid` (`mid`),
KEY `begintime` (`begintime`),
KEY `dg` (`day`,`group`),
KEY `td` (`tid`,`day`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
Then look at the following sql:
SELECT round FROM arena_match_index WHERE `day` = '2010-12-31' AND `group` = 18 AND `begintime` < '2010-12-31 12:14:28' order by begintime LIMIT 1;
This article sql The query condition of shows that the possible indexes used are `begintime` and `dg`, But because of use order by begintime Sort mysql Last choice use `begintime` Indexes ,explain As the result of the :
mysql> explain SELECT round FROM arena_match_index WHERE `day` = '2010-12-31' AND `group` = 18 AND `begintime` < '2010-12-31 12:14:28' order by begintime LIMIT 1;
+----+-------------+-------------------+-------+---------------+-----------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+-------+---------------+-----------+---------+------+--------+-------------+
| 1 | SIMPLE | arena_match_index | range | begintime,dg | begintime | 8 | NULL | 226480 | Using where |
+----+-------------+-------------------+-------+---------------+-----------+---------+------+--------+-------------+
explain The results of display use `begintime` Index to scan 22w Bar record , Such query performance is very poor , The actual implementation is also the first implementation ( When there is no cached data ) The need when 30 Seconds or more . In fact, this query uses `dg` The performance of Federated index is better , Because there are only dozens of games in the same group on the same day , Therefore, priority should be given to `dg` The index locates the matching data set and then sorts , So how to tell mysql Use the specified index ? Use use index sentence :
mysql> explain SELECT round FROM arena_match_index use index (dg) WHERE `day` = '2010-12-31' AND `group` = 18 AND `begintime` < '2010-12-31 12:14:28' order by begintime LIMIT 1;
+----+-------------+-------------------+------+---------------+------+---------+-------------+------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+------+---------------+------+---------+-------------+------+-----------------------------+
| 1 | SIMPLE | arena_match_index | ref | dg | dg | 7 | const,const | 757 | Using where; Using filesort |
+----+-------------+-------------------+------+---------------+------+---------+-------------+------+-----------------------------+
explain The results show that using `dg` The federated index only needs to be scanned 757 Data , The performance has been directly improved hundreds of times , The actual execution also returns the query results almost immediately .
In the initial query statement, just put order by begintime Get rid of ,mysql Will use `dg` Index , It is confirmed again order by It will affect mysql Index selection strategy !
mysql> explain SELECT round FROM arena_match_index WHERE `day` = '2010-12-31' AND `group` = 18 AND `begintime` < '2010-12-31 12:14:28' LIMIT 1;
+----+-------------+-------------------+------+---------------+------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+------+---------------+------+---------+-------------+------+-------------+
| 1 | SIMPLE | arena_match_index | ref | begintime,dg | dg | 7 | const,const | 717 | Using where |
+----+-------------+-------------------+------+---------------+------+---------+-------------+------+-------------+
Say through the above example mysql Sometimes it's not smart , Not always make the best choice , We still need developers to do it “ Training ”!
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/111652.html Link to the original text :https://javaforall.cn
边栏推荐
- Two ways to monitor the change of user points
- QT note 2
- 23.2 customizing the banner control display hidden banner modify banner
- Beauty naked chat for a while, naked chat over the crematorium!
- A summary of practical websites that won't brighten people's eyes
- Summary of common skills
- Kotlin variables and constants
- Redis进阶
- Oracle 19C OCP 1z0-082 certification examination question bank (51-60)
- Grid segmentation
猜你喜欢
[freeswitch development practice] use SIP client Yate to connect freeswitch for VoIP calls
Basic music theory rhythm connection problem, very important
【搜索专题】看完必会的搜索问题之洪水覆盖
2022-7-8 personal qualifying 5 competition experience (supplementary)
Human computer interaction software based on C language
有限元学习知识点备案
Mycat2 sub database and sub table
Xshell batch send command to multiple sessions
基于C语言设计的换乘指南打印系统
Special lecture 2 dynamic planning learning experience (should be updated for a long time)
随机推荐
Oracle 19C OCP 1z0-082 certification examination question bank (24-29)
Excel find duplicate lines
Spark scheduling analysis
If Yi Lijing spits about programmers
Poor English, Oracle OCP or MySQL OCP exam can also get a high score of 80 points
23.8 using the applicationrunner or commandlinerunner to implement applicationrunner and commandlinerunner
QT note 2
JS工具函数大全
基于C语言实现的人机交互软件
为什么要在时钟输出上预留电容的工位?
Kotlin program control
P1825 [USACO11OPEN]Corn Maze S
Oracle 19C OCP 1z0-082 certification examination question bank (51-60)
Guitar staff link Jasmine
Super nice navigation page (static page)
Daily Note (11) -- word formula input arbitrary matrix
Mysql database connection / query index and other common syntax
QT note 1
The data read by Flink Oracle CDC is always null. Do you know
Oracle 19C OCP 1z0-082 certification examination question bank (7-12)