大家,我的一个数据表里32320多记录,用select加上where条件查询用了56032毫秒,select语句为
select row,col,pic from map17 where col<6 and row < 6;
然后建了一个索引。建索引语句:create index query on map (row,col,pic(1000));
测试后还需要7453毫秒,任然很慢,求大神分析一下,怎么提高select(查询速度)
数据表map的表结构:
Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| col | int(11) | YES | | NULL | |
| row | int(11) | YES | MUL | NULL | |
| lat | double | YES | | NULL | |
| lon | double | YES | | NULL | |
| picname | varchar(255) | YES | | NULL | |
| pic | blob | YES | | NULL | |
+---------+--------------+------+-----+---------+-------------
---+查询性能select测试索引
select row,col,pic from map17 where col<6 and row < 6;
然后建了一个索引。建索引语句:create index query on map (row,col,pic(1000));
测试后还需要7453毫秒,任然很慢,求大神分析一下,怎么提高select(查询速度)
数据表map的表结构:
Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| col | int(11) | YES | | NULL | |
| row | int(11) | YES | MUL | NULL | |
| lat | double | YES | | NULL | |
| lon | double | YES | | NULL | |
| picname | varchar(255) | YES | | NULL | |
| pic | blob | YES | | NULL | |
+---------+--------------+------+-----+---------+-------------
---+查询性能select测试索引
索引对你这个语句不起作用 - - .
任何范围条件的右边无法被优化 ( In 这种相当于多个相等条件例外 ) .
你这个表分片了 ?
不然何来 map17 ?
分片数量还不少啊 .
create index query on map (row,col,pic(1000));
这个 , 索引记录的太多了 , 你可以算一下索引基数 , 不用一下索引愣多 .
而且 , WHERE 条件需要根据索引的顺序来 .
另外你这个 ROW 和 COL ,起码 ROW ,索引基数一定要高 .
你的| picname | varchar(255) | YES | | NULL |
设置好大啊3.客户端传过来的表字符集是否与数据库字符集一样,如果不一样在操作的时候mysql会进行转换的,会占用更多的空间
ROW、COL有多少种值
结果:*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: MAP17
type: range
possible_keys: query
key: query
key_len: 4
ref: NULL
rows: 4950
Extra: Using where
1 row in set (0.00 sec)
将语句改成
SELECT ROW,COL,PIC FROM MAP17 WHERE ROW in(1,2,3,4,5) AND COL < 6
再发 EXPLAIN 结果
另外请告诉我
SET GLOBAL profiling=1
SET profiling=1
SELECT SQL_NO_CACHE ROW,COL,PIC FROM MAP17 WHERE ROW in(1,2,3,4,5) AND COL < 6
show profile for query x
的结果,让我们具体看一下性能在哪里消耗掉的 , 我那个 x 是指第几个查询 .
结果:*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: MAP17
type: range
possible_keys: query
key: query
key_len: 8
ref: NULL
rows: 24
Extra: Using where
1 row in set (0.00 sec)mysql> select @@profiling
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)-----+-----+-----+
| ROW | COL | PIC |
+-----+-----+-----+
| 1 | 0 | |
| 1 | 1 | |
| 1 | 2 | |
| 1 | 3 | |
| 1 | 4 | |
| 1 | 5 | |
| 2 | 0 | |
| 2 | 1 | |
| 2 | 2 | |
| 2 | 3 | |
| 2 | 4 | |
| 2 | 5 | |
| 3 | 0 | |
| 3 | 1 | |
| 3 | 2 | |
| 3 | 3 | |
| 3 | 4 | |
| 3 | 5 | |
| 4 | 0 | |
| 4 | 1 | |
| 4 | 2 | |
| 4 | 3 | |
| 4 | 4 | |
| 4 | 5 | |
| 5 | 0 | |
| 5 | 1 | |
| 5 | 2 | |
| 5 | 3 | |
| 5 | 4 | |
| 5 | 5 | |
+-----+-----+-----+
30 rows in set
mysql> show profiles;
+----------+-----------+--------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+-----------+--------------------------------------------------------------------------------+
| 1 | 0.0001365 | select @@profiling |
| 2 | 8.75E-5 | set grobal profiling = 1 |
| 3 | 0.0001385 | set global profiling = 1 |
| 4 | 0.020536 | SELECT SQL_NO_CACHE ROW,COL,PIC FROM MAP17 WHERE ROW in(1,2,3,4,5) AND COL < 6 |
+----------+-----------+--------------------------------------------------------------------------------+
4 rows in setmysql> show pro
file for query 3;
+--------------------+----------+
| Status | Duration |
+--------------------+----------+
| starting | 5.9E-5 |
| Opening tables | 1.6E-5 |
| freeing items | 5.8E-5 |
| logging slow query | 4E-6 |
| cleaning up | 3E-6 |
+--------------------+----------+
结果:*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: MAP17
type: range
possible_keys: query
key: query
key_len: 8
ref: NULL
rows: 24
Extra: Using where
1 row in set (0.00 sec)mysql> select @@profiling
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)-----+-----+-----+
| ROW | COL | PIC |
+-----+-----+-----+
| 1 | 0 | |
| 1 | 1 | |
| 1 | 2 | |
| 1 | 3 | |
| 1 | 4 | |
| 1 | 5 | |
| 2 | 0 | |
| 2 | 1 | |
| 2 | 2 | |
| 2 | 3 | |
| 2 | 4 | |
| 2 | 5 | |
| 3 | 0 | |
| 3 | 1 | |
| 3 | 2 | |
| 3 | 3 | |
| 3 | 4 | |
| 3 | 5 | |
| 4 | 0 | |
| 4 | 1 | |
| 4 | 2 | |
| 4 | 3 | |
| 4 | 4 | |
| 4 | 5 | |
| 5 | 0 | |
| 5 | 1 | |
| 5 | 2 | |
| 5 | 3 | |
| 5 | 4 | |
| 5 | 5 | |
+-----+-----+-----+
30 rows in set
mysql> show profiles;
+----------+-----------+--------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+-----------+--------------------------------------------------------------------------------+
| 1 | 0.0001365 | select @@profiling |
| 2 | 8.75E-5 | set grobal profiling = 1 |
| 3 | 0.0001385 | set global profiling = 1 |
| 4 | 0.020536 | SELECT SQL_NO_CACHE ROW,COL,PIC FROM MAP17 WHERE ROW in(1,2,3,4,5) AND COL < 6 |
+----------+-----------+--------------------------------------------------------------------------------+
4 rows in setmysql> show pro
file for query 3;
+--------------------+----------+
| Status | Duration |
+--------------------+----------+
| starting | 5.9E-5 |
| Opening tables | 1.6E-5 |
| freeing items | 5.8E-5 |
| logging slow query | 4E-6 |
| cleaning up | 3E-6 |
+--------------------+----------+那看来哥我帮你解决了这问题了?
你现在的 ROW 只有 24 了,那么时间应该不超过 0.05S 了?
语句:EXPLAIN SELECT ROW,COL,PIC FROM MAP17 WHERE ROW in(1,2,3,4,5) AND COL < 6
结果:*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: MAP17
type: range
possible_keys: query
key: query
key_len: 8
ref: NULL
rows: 24
Extra: Using where
1 row in set (0.00 sec)mysql> select @@profiling
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)-----+-----+-----+
| ROW | COL | PIC |
+-----+-----+-----+
| 1 | 0 | |
| 1 | 1 | |
| 1 | 2 | |
| 1 | 3 | |
| 1 | 4 | |
| 1 | 5 | |
| 2 | 0 | |
| 2 | 1 | |
| 2 | 2 | |
| 2 | 3 | |
| 2 | 4 | |
| 2 | 5 | |
| 3 | 0 | |
| 3 | 1 | |
| 3 | 2 | |
| 3 | 3 | |
| 3 | 4 | |
| 3 | 5 | |
| 4 | 0 | |
| 4 | 1 | |
| 4 | 2 | |
| 4 | 3 | |
| 4 | 4 | |
| 4 | 5 | |
| 5 | 0 | |
| 5 | 1 | |
| 5 | 2 | |
| 5 | 3 | |
| 5 | 4 | |
| 5 | 5 | |
+-----+-----+-----+
30 rows in set
mysql> show profiles;
+----------+-----------+--------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+-----------+--------------------------------------------------------------------------------+
| 1 | 0.0001365 | select @@profiling |
| 2 | 8.75E-5 | set grobal profiling = 1 |
| 3 | 0.0001385 | set global profiling = 1 |
| 4 | 0.020536 | SELECT SQL_NO_CACHE ROW,COL,PIC FROM MAP17 WHERE ROW in(1,2,3,4,5) AND COL < 6 |
+----------+-----------+--------------------------------------------------------------------------------+
4 rows in setmysql> show pro
file for query 3;
+--------------------+----------+
| Status | Duration |
+--------------------+----------+
| starting | 5.9E-5 |
| Opening tables | 1.6E-5 |
| freeing items | 5.8E-5 |
| logging slow query | 4E-6 |
| cleaning up | 3E-6 |
+--------------------+----------+那看来哥我帮你解决了这问题了?
你现在的 ROW 只有 24 了,那么时间应该不超过 0.05S 了?
呵呵,625毫秒
结果:*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: MAP17
type: range
possible_keys: query
key: query
key_len: 8
ref: NULL
rows: 24
Extra: Using where
1 row in set (0.00 sec)mysql> select @@profiling
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)-----+-----+-----+
| ROW | COL | PIC |
+-----+-----+-----+
| 1 | 0 | |
| 1 | 1 | |
| 1 | 2 | |
| 1 | 3 | |
| 1 | 4 | |
| 1 | 5 | |
| 2 | 0 | |
| 2 | 1 | |
| 2 | 2 | |
| 2 | 3 | |
| 2 | 4 | |
| 2 | 5 | |
| 3 | 0 | |
| 3 | 1 | |
| 3 | 2 | |
| 3 | 3 | |
| 3 | 4 | |
| 3 | 5 | |
| 4 | 0 | |
| 4 | 1 | |
| 4 | 2 | |
| 4 | 3 | |
| 4 | 4 | |
| 4 | 5 | |
| 5 | 0 | |
| 5 | 1 | |
| 5 | 2 | |
| 5 | 3 | |
| 5 | 4 | |
| 5 | 5 | |
+-----+-----+-----+
30 rows in set
mysql> show profiles;
+----------+-----------+--------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+-----------+--------------------------------------------------------------------------------+
| 1 | 0.0001365 | select @@profiling |
| 2 | 8.75E-5 | set grobal profiling = 1 |
| 3 | 0.0001385 | set global profiling = 1 |
| 4 | 0.020536 | SELECT SQL_NO_CACHE ROW,COL,PIC FROM MAP17 WHERE ROW in(1,2,3,4,5) AND COL < 6 |
+----------+-----------+--------------------------------------------------------------------------------+
4 rows in setmysql> show pro
file for query 3;
+--------------------+----------+
| Status | Duration |
+--------------------+----------+
| starting | 5.9E-5 |
| Opening tables | 1.6E-5 |
| freeing items | 5.8E-5 |
| logging slow query | 4E-6 |
| cleaning up | 3E-6 |
+--------------------+----------+那看来哥我帮你解决了这问题了?
你现在的 ROW 只有 24 了,那么时间应该不超过 0.05S 了?
呵呵,625毫秒
你那配置楞差啊,还是你是在生产环境服务器上测服务器本生就繁忙?并发访问很多?
SHOW FULL PROCESSLIST; 很多?
还是机器负载接近天花板?
3.2 万条,检索 24 条数据,居然还需要 0.5S 以上?匪夷所思
另外你这个还可以更进一步优化,弄个覆盖索引去吧。我相信再查的机器提高个一倍肯定不是问题
| Status | Duration |
+--------------------+----------+
| starting | 5.9E-5 |
| Opening tables | 1.6E-5 |
| freeing items | 5.8E-5 |
| logging slow query | 4E-6 |
| cleaning up | 3E-6 |
+--------------------+----------+
另外从你的这个过程看来有问题啊。
一 :不应该有 Opening tables 你的 table_cache 设置是不是不够大?你的 open_file_limits(这参数没记得太清楚 ).
二 :你这个 freeing items 状态不应该出现 , 如果你的 THREADS 状态多有出现这东西,找找原因吧 。如果偶有出现 ,无妨 。
三 :你这里已经 LOGGING SLOW QUERY 了 。那么你去看看吧,一般来说慢查询日志第二行记录四个信息 。
等待时间 ,执行时间 ,检查条数 ,返回条数 。
你去看一下你等待时间吧,可以确定的是 24 条检索这么小的量,不应该需要那么长时间 。
结果:*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: MAP17
type: range
possible_keys: query
key: query
key_len: 8
ref: NULL
rows: 24
Extra: Using where
1 row in set (0.00 sec)mysql> select @@profiling
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)-----+-----+-----+
| ROW | COL | PIC |
+-----+-----+-----+
| 1 | 0 | |
| 1 | 1 | |
| 1 | 2 | |
| 1 | 3 | |
| 1 | 4 | |
| 1 | 5 | |
| 2 | 0 | |
| 2 | 1 | |
| 2 | 2 | |
| 2 | 3 | |
| 2 | 4 | |
| 2 | 5 | |
| 3 | 0 | |
| 3 | 1 | |
| 3 | 2 | |
| 3 | 3 | |
| 3 | 4 | |
| 3 | 5 | |
| 4 | 0 | |
| 4 | 1 | |
| 4 | 2 | |
| 4 | 3 | |
| 4 | 4 | |
| 4 | 5 | |
| 5 | 0 | |
| 5 | 1 | |
| 5 | 2 | |
| 5 | 3 | |
| 5 | 4 | |
| 5 | 5 | |
+-----+-----+-----+
30 rows in set
mysql> show profiles;
+----------+-----------+--------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+-----------+--------------------------------------------------------------------------------+
| 1 | 0.0001365 | select @@profiling |
| 2 | 8.75E-5 | set grobal profiling = 1 |
| 3 | 0.0001385 | set global profiling = 1 |
| 4 | 0.020536 | SELECT SQL_NO_CACHE ROW,COL,PIC FROM MAP17 WHERE ROW in(1,2,3,4,5) AND COL < 6 |
+----------+-----------+--------------------------------------------------------------------------------+
4 rows in setmysql> show pro
file for query 3;
+--------------------+----------+
| Status | Duration |
+--------------------+----------+
| starting | 5.9E-5 |
| Opening tables | 1.6E-5 |
| freeing items | 5.8E-5 |
| logging slow query | 4E-6 |
| cleaning up | 3E-6 |
+--------------------+----------+那看来哥我帮你解决了这问题了?
你现在的 ROW 只有 24 了,那么时间应该不超过 0.05S 了?
呵呵,625毫秒
你那配置楞差啊,还是你是在生产环境服务器上测服务器本生就繁忙?并发访问很多?
SHOW FULL PROCESSLIST; 很多?
还是机器负载接近天花板?
3.2 万条,检索 24 条数据,居然还需要 0.5S 以上?匪夷所思
另外你这个还可以更进一步优化,弄个覆盖索引去吧。我相信再查的机器提高个一倍肯定不是问题看来你是数据库大牛啊,我说一下,一个记录数只有3万多条的数据表,不加索引,select一下要十多秒,你信吗?再想问一句,你懂什么是索引吗,你懂索引的作用吗,你懂加与不加索引,数据库里面的select是怎么进行查询的吗,不懂就别胡扯,跟你讲,你的猜测全是错的
结果:*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: MAP17
type: range
possible_keys: query
key: query
key_len: 8
ref: NULL
rows: 24
Extra: Using where
1 row in set (0.00 sec)mysql> select @@profiling
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)-----+-----+-----+
| ROW | COL | PIC |
+-----+-----+-----+
| 1 | 0 | |
| 1 | 1 | |
| 1 | 2 | |
| 1 | 3 | |
| 1 | 4 | |
| 1 | 5 | |
| 2 | 0 | |
| 2 | 1 | |
| 2 | 2 | |
| 2 | 3 | |
| 2 | 4 | |
| 2 | 5 | |
| 3 | 0 | |
| 3 | 1 | |
| 3 | 2 | |
| 3 | 3 | |
| 3 | 4 | |
| 3 | 5 | |
| 4 | 0 | |
| 4 | 1 | |
| 4 | 2 | |
| 4 | 3 | |
| 4 | 4 | |
| 4 | 5 | |
| 5 | 0 | |
| 5 | 1 | |
| 5 | 2 | |
| 5 | 3 | |
| 5 | 4 | |
| 5 | 5 | |
+-----+-----+-----+
30 rows in set
mysql> show profiles;
+----------+-----------+--------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+-----------+--------------------------------------------------------------------------------+
| 1 | 0.0001365 | select @@profiling |
| 2 | 8.75E-5 | set grobal profiling = 1 |
| 3 | 0.0001385 | set global profiling = 1 |
| 4 | 0.020536 | SELECT SQL_NO_CACHE ROW,COL,PIC FROM MAP17 WHERE ROW in(1,2,3,4,5) AND COL < 6 |
+----------+-----------+--------------------------------------------------------------------------------+
4 rows in setmysql> show pro
file for query 3;
+--------------------+----------+
| Status | Duration |
+--------------------+----------+
| starting | 5.9E-5 |
| Opening tables | 1.6E-5 |
| freeing items | 5.8E-5 |
| logging slow query | 4E-6 |
| cleaning up | 3E-6 |
+--------------------+----------+那看来哥我帮你解决了这问题了?
你现在的 ROW 只有 24 了,那么时间应该不超过 0.05S 了?
呵呵,625毫秒
你那配置楞差啊,还是你是在生产环境服务器上测服务器本生就繁忙?并发访问很多?
SHOW FULL PROCESSLIST; 很多?
还是机器负载接近天花板?
3.2 万条,检索 24 条数据,居然还需要 0.5S 以上?匪夷所思
另外你这个还可以更进一步优化,弄个覆盖索引去吧。我相信再查的机器提高个一倍肯定不是问题看来你是数据库大牛啊,我说一下,一个记录数只有3万多条的数据表,不加索引,select一下要十多秒,你信吗?再想问一句,你懂什么是索引吗,你懂索引的作用吗,你懂加与不加索引,数据库里面的select是怎么进行查询的吗,不懂就别胡扯,跟你讲,你的猜测全是错的写错了,不是十多秒 ,是五十多秒
你的SELECT是在MAP17表中,索引建在MAP表。没什么意义啊。
建议贴出以下内容以供分析。 不要用/Gexplain select row,col,pic from map17 where col<6 and row < 6;
show index from map17;
结果:*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: MAP17
type: range
possible_keys: query
key: query
key_len: 8
ref: NULL
rows: 24
Extra: Using where
1 row in set (0.00 sec)mysql> select @@profiling
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)-----+-----+-----+
| ROW | COL | PIC |
+-----+-----+-----+
| 1 | 0 | |
| 1 | 1 | |
| 1 | 2 | |
| 1 | 3 | |
| 1 | 4 | |
| 1 | 5 | |
| 2 | 0 | |
| 2 | 1 | |
| 2 | 2 | |
| 2 | 3 | |
| 2 | 4 | |
| 2 | 5 | |
| 3 | 0 | |
| 3 | 1 | |
| 3 | 2 | |
| 3 | 3 | |
| 3 | 4 | |
| 3 | 5 | |
| 4 | 0 | |
| 4 | 1 | |
| 4 | 2 | |
| 4 | 3 | |
| 4 | 4 | |
| 4 | 5 | |
| 5 | 0 | |
| 5 | 1 | |
| 5 | 2 | |
| 5 | 3 | |
| 5 | 4 | |
| 5 | 5 | |
+-----+-----+-----+
30 rows in set
mysql> show profiles;
+----------+-----------+--------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+-----------+--------------------------------------------------------------------------------+
| 1 | 0.0001365 | select @@profiling |
| 2 | 8.75E-5 | set grobal profiling = 1 |
| 3 | 0.0001385 | set global profiling = 1 |
| 4 | 0.020536 | SELECT SQL_NO_CACHE ROW,COL,PIC FROM MAP17 WHERE ROW in(1,2,3,4,5) AND COL < 6 |
+----------+-----------+--------------------------------------------------------------------------------+
4 rows in setmysql> show pro
file for query 3;
+--------------------+----------+
| Status | Duration |
+--------------------+----------+
| starting | 5.9E-5 |
| Opening tables | 1.6E-5 |
| freeing items | 5.8E-5 |
| logging slow query | 4E-6 |
| cleaning up | 3E-6 |
+--------------------+----------+那看来哥我帮你解决了这问题了?
你现在的 ROW 只有 24 了,那么时间应该不超过 0.05S 了?
呵呵,625毫秒
你那配置楞差啊,还是你是在生产环境服务器上测服务器本生就繁忙?并发访问很多?
SHOW FULL PROCESSLIST; 很多?
还是机器负载接近天花板?
3.2 万条,检索 24 条数据,居然还需要 0.5S 以上?匪夷所思
另外你这个还可以更进一步优化,弄个覆盖索引去吧。我相信再查的机器提高个一倍肯定不是问题看来你是数据库大牛啊,我说一下,一个记录数只有3万多条的数据表,不加索引,select一下要十多秒,你信吗?再想问一句,你懂什么是索引吗,你懂索引的作用吗,你懂加与不加索引,数据库里面的select是怎么进行查询的吗,不懂就别胡扯,跟你讲,你的猜测全是错的写错了,不是十多秒 ,是五十多秒
匪夷所思,孩子,索引第三个字段是 pic(1000)
既然设 pic(1000) 证明第三个字段平均存储内容大于 1000 , 我推断 varchar(8000) 不过分,既然设定了前缀长度 1000 证明大多数条目都大于 1K . 你去一个 3 万多条平均行大小大于 5K 的数据库来个 SELECT * ,不要 10 多秒就见鬼了 。
而且,孩子,没看到 EXPLAIN ROW 列检测的条目是 24 了?
用事实说话 。我本来看到你的帖子都想笑 。
毫无证据的喷我 。这年头网上喷子怎么这么多啊,哈哈哈哈 。
我不是大牛,我可以确定你一定是个小盆友 。
结果:*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: MAP17
type: range
possible_keys: query
key: query
key_len: 8
ref: NULL
rows: 24
Extra: Using where
1 row in set (0.00 sec)mysql> select @@profiling
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)-----+-----+-----+
| ROW | COL | PIC |
+-----+-----+-----+
| 1 | 0 | |
| 1 | 1 | |
| 1 | 2 | |
| 1 | 3 | |
| 1 | 4 | |
| 1 | 5 | |
| 2 | 0 | |
| 2 | 1 | |
| 2 | 2 | |
| 2 | 3 | |
| 2 | 4 | |
| 2 | 5 | |
| 3 | 0 | |
| 3 | 1 | |
| 3 | 2 | |
| 3 | 3 | |
| 3 | 4 | |
| 3 | 5 | |
| 4 | 0 | |
| 4 | 1 | |
| 4 | 2 | |
| 4 | 3 | |
| 4 | 4 | |
| 4 | 5 | |
| 5 | 0 | |
| 5 | 1 | |
| 5 | 2 | |
| 5 | 3 | |
| 5 | 4 | |
| 5 | 5 | |
+-----+-----+-----+
30 rows in set
mysql> show profiles;
+----------+-----------+--------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+-----------+--------------------------------------------------------------------------------+
| 1 | 0.0001365 | select @@profiling |
| 2 | 8.75E-5 | set grobal profiling = 1 |
| 3 | 0.0001385 | set global profiling = 1 |
| 4 | 0.020536 | SELECT SQL_NO_CACHE ROW,COL,PIC FROM MAP17 WHERE ROW in(1,2,3,4,5) AND COL < 6 |
+----------+-----------+--------------------------------------------------------------------------------+
4 rows in setmysql> show pro
file for query 3;
+--------------------+----------+
| Status | Duration |
+--------------------+----------+
| starting | 5.9E-5 |
| Opening tables | 1.6E-5 |
| freeing items | 5.8E-5 |
| logging slow query | 4E-6 |
| cleaning up | 3E-6 |
+--------------------+----------+那看来哥我帮你解决了这问题了?
你现在的 ROW 只有 24 了,那么时间应该不超过 0.05S 了?
呵呵,625毫秒
你那配置楞差啊,还是你是在生产环境服务器上测服务器本生就繁忙?并发访问很多?
SHOW FULL PROCESSLIST; 很多?
还是机器负载接近天花板?
3.2 万条,检索 24 条数据,居然还需要 0.5S 以上?匪夷所思
另外你这个还可以更进一步优化,弄个覆盖索引去吧。我相信再查的机器提高个一倍肯定不是问题看来你是数据库大牛啊,我说一下,一个记录数只有3万多条的数据表,不加索引,select一下要十多秒,你信吗?再想问一句,你懂什么是索引吗,你懂索引的作用吗,你懂加与不加索引,数据库里面的select是怎么进行查询的吗,不懂就别胡扯,跟你讲,你的猜测全是错的写错了,不是十多秒 ,是五十多秒
匪夷所思,孩子,索引第三个字段是 pic(1000)
既然设 pic(1000) 证明第三个字段平均存储内容大于 1000 , 我推断 varchar(8000) 不过分,既然设定了前缀长度 1000 证明大多数条目都大于 1K . 你去一个 3 万多条平均行大小大于 5K 的数据库来个 SELECT * ,不要 10 多秒就见鬼了 。
而且,孩子,没看到 EXPLAIN ROW 列检测的条目是 24 了?
用事实说话 。我本来看到你的帖子都想笑 。
毫无证据的喷我 。这年头网上喷子怎么这么多啊,哈哈哈哈 。
我不是大牛,我可以确定你一定是个小盆友 。
我就不跟你分析 SELECT 查询过程了,我相信这个任意一本书中都能提高《高性能的 MYSQL》第 一 二 三 版,有关查询优化的章节必须有。另外在姜承尧的《MYSQL 技术内幕 INNODB 存储引擎》中也有提到 。这个我就不跟你提了 。
你这么喷我我倒觉得很有意思 。我好心来帮你答一下就换来你这么喷人?
况且该用的检测都给你用了,EXPLAIN ,PROFILE 。对于剖析单条 SQL 性能已经足够了 。
况且,根据我的经验 90% 的时间一般花在确定问题在哪 ,10% 的时间花在解决问题 。
一条语句执行时间长,生产环境中,就算 SYSTEM 等级的查询执行时间都可能超过 1 个小时,这没什么 。
查询时间分为两部分,等待时间和执行时间 。
慢查日志中第二行第一个参数就是等待时间孩子从来没看过?
一条语句执行时间很可能是 0.01S 等待时间超过 1 个小时,这在未优化的生产环境中非常正常 。
- - .
哎,狗咬吕洞宾啊 。
自己贴出来的 EXPLAIN ROW 行 24 ,开始的 4900 多 。
居然直接就喷人,悲剧啊,哈哈哈哈
结果:*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: MAP17
type: range
possible_keys: query
key: query
key_len: 8
ref: NULL
rows: 24
Extra: Using where
1 row in set (0.00 sec)mysql> select @@profiling
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)-----+-----+-----+
| ROW | COL | PIC |
+-----+-----+-----+
| 1 | 0 | |
| 1 | 1 | |
| 1 | 2 | |
| 1 | 3 | |
| 1 | 4 | |
| 1 | 5 | |
| 2 | 0 | |
| 2 | 1 | |
| 2 | 2 | |
| 2 | 3 | |
| 2 | 4 | |
| 2 | 5 | |
| 3 | 0 | |
| 3 | 1 | |
| 3 | 2 | |
| 3 | 3 | |
| 3 | 4 | |
| 3 | 5 | |
| 4 | 0 | |
| 4 | 1 | |
| 4 | 2 | |
| 4 | 3 | |
| 4 | 4 | |
| 4 | 5 | |
| 5 | 0 | |
| 5 | 1 | |
| 5 | 2 | |
| 5 | 3 | |
| 5 | 4 | |
| 5 | 5 | |
+-----+-----+-----+
30 rows in set
mysql> show profiles;
+----------+-----------+--------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+-----------+--------------------------------------------------------------------------------+
| 1 | 0.0001365 | select @@profiling |
| 2 | 8.75E-5 | set grobal profiling = 1 |
| 3 | 0.0001385 | set global profiling = 1 |
| 4 | 0.020536 | SELECT SQL_NO_CACHE ROW,COL,PIC FROM MAP17 WHERE ROW in(1,2,3,4,5) AND COL < 6 |
+----------+-----------+--------------------------------------------------------------------------------+
4 rows in setmysql> show pro
file for query 3;
+--------------------+----------+
| Status | Duration |
+--------------------+----------+
| starting | 5.9E-5 |
| Opening tables | 1.6E-5 |
| freeing items | 5.8E-5 |
| logging slow query | 4E-6 |
| cleaning up | 3E-6 |
+--------------------+----------+那看来哥我帮你解决了这问题了?
你现在的 ROW 只有 24 了,那么时间应该不超过 0.05S 了?
呵呵,625毫秒
你那配置楞差啊,还是你是在生产环境服务器上测服务器本生就繁忙?并发访问很多?
SHOW FULL PROCESSLIST; 很多?
还是机器负载接近天花板?
3.2 万条,检索 24 条数据,居然还需要 0.5S 以上?匪夷所思
另外你这个还可以更进一步优化,弄个覆盖索引去吧。我相信再查的机器提高个一倍肯定不是问题看来你是数据库大牛啊,我说一下,一个记录数只有3万多条的数据表,不加索引,select一下要十多秒,你信吗?再想问一句,你懂什么是索引吗,你懂索引的作用吗,你懂加与不加索引,数据库里面的select是怎么进行查询的吗,不懂就别胡扯,跟你讲,你的猜测全是错的写错了,不是十多秒 ,是五十多秒
匪夷所思,孩子,索引第三个字段是 pic(1000)
既然设 pic(1000) 证明第三个字段平均存储内容大于 1000 , 我推断 varchar(8000) 不过分,既然设定了前缀长度 1000 证明大多数条目都大于 1K . 你去一个 3 万多条平均行大小大于 5K 的数据库来个 SELECT * ,不要 10 多秒就见鬼了 。
而且,孩子,没看到 EXPLAIN ROW 列检测的条目是 24 了?
用事实说话 。我本来看到你的帖子都想笑 。
毫无证据的喷我 。这年头网上喷子怎么这么多啊,哈哈哈哈 。
我不是大牛,我可以确定你一定是个小盆友 。
对了,你说 50 多秒我不信,我觉得应该是 5000 多秒,我信,哈哈哈哈。你万一是 pic VARCHAR(65535) 呢?每个字段大小都是满满的,50 多秒岂能信?
加上个等待时间,不要个 50000 多秒谁信啊?哈哈哈哈 。
你的SELECT是在MAP17表中,索引建在MAP表。没什么意义啊。
建议贴出以下内容以供分析。 不要用/Gexplain select row,col,pic from map17 where col<6 and row < 6;
show index from map17;不好意思,不是map是map17少些了”17“,索引还是建在map7里的
mysql> explain select row,col,pic from map17 where col<6 and row < 6;+----+-------------+-------+-------+---------------+-------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+-------+---------+------+------+-------------+
| 1 | SIMPLE | map17 | range | query | query | 4 | NULL | 4950 | Using where |
+----+-------------+-------+-------+---------------+-------+---------+------+------+-------------+
1 row in setmysql> show index from map17;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| map17 | 0 | PRIMARY | 1 | id | A | 200873 | NULL | NULL | | BTREE | |
| map17 | 1 | query | 1 | row | A | 14 | NULL | NULL | | BTREE | |
| map17 | 1 | query | 2 | col | A | 50218 | NULL | NULL | | BTREE | |
| map17 | 1 | query | 3 | pic | A | 50218 | 767 | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
4 rows in set
mysql> show index from map17;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| map17 | 0 | PRIMARY | 1 | id | A | 237093 | NULL | NULL | | BTREE | |
| map17 | 1 | row_col | 1 | row | A | 74 | NULL | NULL | | BTREE | |
| map17 | 1 | row_col | 2 | col | A | 79031 | NULL | NULL | | BTREE | |
| map17 | 1 | col_row | 1 | col | A | 1693 | NULL | NULL | | BTREE | |
| map17 | 1 | col_row | 2 | row | A | 79031 | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
5 rows in setmysql> explain select row,col,pic from map17 where row<8 and col <8;
+----+-------------+-------+-------+-----------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-----------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | map17 | range | row_col,col_row | col_row | 4 | NULL | 336 | Using where |
+----+-------------+-------+-------+-----------------+---------+---------+------+------+-------------+
1 row in setmysql> explain select row,col,pic from map17 where row<8 and col =8;
+----+-------------+-------+-------+-----------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-----------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | map17 | range | row_col,col_row | col_row | 8 | NULL | 7 | Using where |
+----+-------------+-------+-------+-----------------+---------+---------+------+------+-------------+
1 row in setmysql> explain select row,col,pic from map17 where row=8 and col <8;
+----+-------------+-------+-------+-----------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-----------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | map17 | range | row_col,col_row | row_col | 8 | NULL | 7 | Using where |
+----+-------------+-------+-------+-----------------+---------+---------+------+------+-------------+
1 row in setmysql> explain select row,col,pic from map17 where row>30 and row<38 and col=3456;
+----+-------------+-------+-------+-----------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-----------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | map17 | range | row_col,col_row | col_row | 8 | NULL | 10 | Using where |
+----+-------------+-------+-------+-----------------+---------+---------+------+------+-------------+
1 row in setmysql> explain select row,col,pic from map17 where row=38 and col>=3456 and col <3464;
+----+-------------+-------+-------+-----------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-----------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | map17 | range | row_col,col_row | row_col | 8 | NULL | 10 | Using where |
+----+-------------+-------+-------+-----------------+---------+---------+------+------+-------------+
1 row in set是不是就这条语句慢了点
select row,col,pic from map17 where row<8 and col <8