我有一个遗留系统的表,没有主键.它记录了工厂发布材料的交易数据.
为简单起见,我们可以说每行包含job_number,part_number,quantity&date_issued.
我在日期发布列中添加了一个索引.当我运行EXPLAIN SELECT*FROM issued_parts WHERE date_issued>'20100101'时,它显示如下:
+----+-------------+----------------+------+-------------------+------+---------+------+---------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------------+------+-------------------+------+---------+------+---------+-------------+ | 1 | SIMPLE | issued_parts | ALL | date_issued_alloc | NULL | NULL | NULL | 9724620 | Using where | +----+-------------+----------------+------+-------------------+------+---------+------+---------+-------------+
所以它看到了关键,但它不使用它?有人可以解释原因吗?
我使用mySQL服务器版本5.5.14,现在我正在使用Explain命令尝试这个简单的SQL查询:
EXPLAIN SELECT id, name, thumb FROM `twitter_profiles` LIMIT 10;
Run Code Online (Sandbox Code Playgroud)
它告诉我这个结果:
+----+-------------+-------+------+---------------+------+---------+------+-------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------+
| 1 | SIMPLE | tp | ALL | NULL | NULL | NULL | NULL | 40823 | |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------+
1 row in set (0.02 sec)
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么它扫描整个表而不是像我在LIMIT子句中指定的前10行?
提前感谢您的建议!
干杯,
的Jakub
我要选择的列的内容text从entrytable.
EXPLAIN SELECT text
FROM entrytable
WHERE user = 'username' &&
`status` = '1' && (
`status_spam_user` = 'no_spam'
|| (
`status_spam_user` = 'neutral' &&
`status_spam_system` = 'neutral'
)
)
ORDER BY datum DESC
LIMIT 6430 , 10
Run Code Online (Sandbox Code Playgroud)
该表有三个指数:
EXPLAIN结果是:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE entrytable ref index_user,index_status_mit_spam index_user 32 const 7800 Using where; Using filesort
Run Code Online (Sandbox Code Playgroud)
possible_keys指数的MySQL可能要使用和keysMySQL的实际使用指标?index_status_mit_spam不使用索引?在查询中,列的顺序与索引中的顺序相同,...index_datum …我有一个表格结构
comment_id primary key
comment_content
comment_author
comment_author_url
Run Code Online (Sandbox Code Playgroud)
当我点火查询时
explain SELECT * FROM comments ORDER BY comment_id
Run Code Online (Sandbox Code Playgroud)
它将结果输出为
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE comments ALL NULL NULL NULL NULL 22563 Using filesort
Run Code Online (Sandbox Code Playgroud)
为什么无法找到我定义为主键的索引?
我一直在玩MySQL上的索引(5.5.24,WinXP),但我找不到服务器在使用时没有使用一个索引的原因LIKE.
这个例子是这样的:
我创建了一个测试表:
create table testTable (
id varchar(50) primary key,
text1 varchar(50) not null,
startDate varchar(50) not null
) ENGINE = innodb;
Run Code Online (Sandbox Code Playgroud)
然后,我添加了一个索引startDate.(请不要问为什么列是文本而不是日期时间..这只是一个简单的测试):
create index jeje on testTable(startdate);
analyze table testTable;
Run Code Online (Sandbox Code Playgroud)
之后,我添加了近200,000行,其中startDate有3个可能的值.(每个人的三分之一出现..近70,000次)
所以,如果我运行这样的EXPLAIN命令:
explain select * from testTable use index (jeje) where startDate = 'aaaaaaaaa';
Run Code Online (Sandbox Code Playgroud)
答案如下:
id = 1
select_type = SIMPLE
type = ref
possible_keys = jeje
key = jeje
rows = 88412
extra = Using where
Run Code Online (Sandbox Code Playgroud)
因此,使用密钥,行数接近200,000/3,所以一切正常.
问题是,如果我将查询更改为:(只需将'='转换为'LIKE'):
explain …Run Code Online (Sandbox Code Playgroud) 当在具有2个值(使用IN或OR结构)的PRIMARY键上进行INNER JOIN时,我在EXPLAIN SELECT中得到"检查每个记录的范围(索引映射:0x1)"
这是查询:
SELECT *
FROM message AS m
INNER JOIN user AS u
ON u.id = m.sender_id OR u.id = m.receiver_id
Run Code Online (Sandbox Code Playgroud)
在做解释时,它给了我:
+----+-------------+-------+------+---------------+------+---------+------+-------+-----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+-------+-----------------------------------------------+
| 1 | SIMPLE | u | ALL | PRIMARY | null | null | null | 75000 | Range checked for each record (index map: 0x1)|
+----+-------------+-------+------+---------------+------+---------+------+-------+-----------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
它不可能......
如果我尝试这个,我会得到相同的结果:
SELECT *
FROM …Run Code Online (Sandbox Code Playgroud) 我知道我可以从查询日志中获取原始查询,粘贴所有绑定变量(也在查询日志中找到),explain在查询前面打上 a,然后直接在 mysql 控制台中运行它以获取解释查询......但是有没有更快的方法来获得解释?
理想情况下,我想做这样的事情:
$query = User::where("favorite_color", "blue");
dd($query->explain());
Run Code Online (Sandbox Code Playgroud)
(显然,实际的查询会更加复杂并且有一些连接)
我尝试添加explain这样的内容:
$query->selectRaw("explain select user.*");
Run Code Online (Sandbox Code Playgroud)
但这导致了一个以以下内容开头的查询:
select explain select...
Run Code Online (Sandbox Code Playgroud)
...这只是无效的sql。
我有一个名为'million_words'的简单表.它有一行有两列 - > id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY&word VARCHAR(50 NOT NULL.
我运行这个查询 - : EXPLAIN SELECT * FROM million_words WHERE word = '-anon'
Extra然后该列打印:'Impossible WHERE noticed after reading const tables,即使该行明显存在于表中.
什么是wronf
我刚刚得到 MySQL Expand 闭包的下一个结果:
不幸的是,我不明白 select_type 列的最后一行中的 MATERIALIZED 是什么意思。
如果有人知道这是什么意思,请给我任何研究这个问题的建议或给我答案。
我正在尝试优化我的查询。
并 为其中一个表获取使用连接缓冲区(块嵌套循环)
EXPLAIN SELECT 命令。
我不知道这是什么意思。我试着谷歌搜索,但我还没有找到解释。