当前位置:网站首页>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=utf8Then 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
边栏推荐
- Uninstallation of dual systems
- 有限元学习知识点备案
- BGP routing principle
- 23.8 using the applicationrunner or commandlinerunner to implement applicationrunner and commandlinerunner
- Nodejs2day (modularization of nodejs, NPM download package, module loading mechanism)
- The second lesson is the construction of development environment
- Flutter text is left aligned with no blank space in the middle
- 【C语言】程序员筑基功法——《函数栈帧的创建与销毁》
- Solve the problem of C # calling form controls across threads
- Inaccurate problem of flutter fijkplayer seekto
猜你喜欢

Maximum common substring & regularity problem

Kotlin variables and constants

6、 Pinda general permission system__ pd-tools-log

Huffman transformation software based on C language

日常一记(11)--word公式输入任意矩阵

Mysql8 dual master and dual slave +mycat2 read / write separation

内存管理-动态分区分配方式模拟

Write common API tools swagger and redoc

OSPF summary

Nodejs2day (modularization of nodejs, NPM download package, module loading mechanism)
随机推荐
Oracle 19C OCP 1z0-082 certification examination question bank (42-50)
23.2 customizing the banner control display hidden banner modify banner
Excel delete blank lines
Kotlin data type
The second lesson is the construction of development environment
Oracle 19C OCP 1z0-083 question bank (1-6)
Oracle 19C OCP 1z0-082 certification examination question bank 1
【时间复杂度空间复杂度】
2022-7-4 personal qualifying 1 competition experience
【FreeSwitch开发实践】使用SIP客户端Yate连接FreeSwitch进行VoIP通话
Special Lecture 3 number theory + game theory learning experience (should be updated for a long time)
Foundry教程:使用多种方式编写可升级的智能合约(上)
Flutter WebView three fingers rush or freeze the screen
Winter vacation homework & Stamp cutting
基于C语言的内存管理-动态分区分配方式模拟
A little awesome, 130000 a month+
Flutter custom player progress bar
六、品达通用权限系统__pd-tools-log
23.8 using the applicationrunner or commandlinerunner to implement applicationrunner and commandlinerunner
基于C#实现的文件管理文件系统