Php 为初学者优化MySQL查询(添加索引、重新编写查询、使用解释等)?

Php 为初学者优化MySQL查询(添加索引、重新编写查询、使用解释等)?,php,mysql,sql,Php,Mysql,Sql,我正在查看慢速查询日志并运行explain on查询,现在如何解释输出以进行改进 示例: EXPLAIN SELECT corecountry, corestatus FROM daydream_ddvalpha.propcore WHERE corecountry = '7' AND corestatus >= '100' 输出: # id, select_type, table, type, possible_keys, key, key_

我正在查看慢速查询日志并运行explain on查询,现在如何解释输出以进行改进

示例:

EXPLAIN SELECT corecountry, corestatus 
        FROM daydream_ddvalpha.propcore 
        WHERE corecountry = '7'    AND corestatus >= '100'

输出:

# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
'1', 'SIMPLE', 'propcore', 'ALL', NULL, NULL, NULL, NULL, '1532', 'Using where'
SHOW INDEX FROM daydream_ddvalpha.propcore = 
# Table, Non_unique, Key_name, Seq_in_index, Column_name, Collation, Cardinality, Sub_part, Packed, Null, Index_type, Comment, Index_comment
'propcore', '0', 'PRIMARY', '1', 'coreref', 'A', '1773', NULL, NULL, ”, 'BTREE', ”, ”

显示索引:

# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
'1', 'SIMPLE', 'propcore', 'ALL', NULL, NULL, NULL, NULL, '1532', 'Using where'
SHOW INDEX FROM daydream_ddvalpha.propcore = 
# Table, Non_unique, Key_name, Seq_in_index, Column_name, Collation, Cardinality, Sub_part, Packed, Null, Index_type, Comment, Index_comment
'propcore', '0', 'PRIMARY', '1', 'coreref', 'A', '1773', NULL, NULL, ”, 'BTREE', ”, ”
描述:

describe daydream_ddvalpha.propcore
# Field, Type, Null, Key, Default, Extra
'coreref', 'varchar(10)', 'NO', 'PRI', '', ''
'coretitle', 'varchar(75)', 'NO', '', '', ''
'coreprice', 'int(25) unsigned', 'NO', '', '0', ''
'corecurr', 'tinyint(1)', 'NO', '', '0', ''
'coreagent', 'varchar(10)', 'NO', '', '0', ''
'corebuild', 'smallint(4)', 'NO', '', '0', ''
'coretown', 'varchar(25)', 'NO', '', '', ''
'coreregion', 'varchar(25)', 'NO', '', '', ''
'corecountry', 'smallint(4)', 'NO', '', '0', ''
'corelocation', 'smallint(4)', 'NO', '', '0', ''
'corestatus', 'smallint(4)', 'NO', '', '0', ''
'corelistsw', 'char(1)', 'NO', '', '', ''
'corepstatus', 'tinyint(4)', 'NO', '', '0', ''
'coreseq', 'mediumint(10)', 'NO', '', '0', ''
'coreviews', 'mediumint(10)', 'NO', '', '0', ''
'coreextract', 'char(1)', 'NO', '', 'n', ''
编辑:新示例

我发现了一个更复杂的查询:

EXPLAIN SELECT coreref, coretitle, coreprice, corecurr, corebuild, coretown,    corecountry, corepstatus, corestatus FROM daydream_ddvalpha.propcore 

    WHERE coretown = 'Torrepacheco' 

    AND corestatus >= '100'

    ORDER BY coreprice ASC

    LIMIT 135, 10
输出:

# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
'1', 'SIMPLE', 'propcore', 'ALL', NULL, NULL, NULL, NULL, '1579', 'Using where; Using    filesort'
我理解第一个例子给出的关于索引的答案,但是这个呢?我是否应该创建一个涵盖coretown、corestatus和coreprice的索引。我有一种感觉,我最终会得到许多具有重复值的索引,或者这是正常的吗?

这是您的查询:

SELECT corecountry, corestatus
FROM daydream_ddvalpha.propcore
WHERE corecountry = '7' AND corestatus >= '100'
where
子句中有两个条件。一是平等,一不是平等。有帮助的索引是daydream\u ddvalpha.propcore(corecountry,corestatus)
corecountry
必须先行,因为相等条件必须是索引中最左边的列。然后得到一个不等式,它是
corecondry

您只选择这两个字段。上面的索引被称为查询的覆盖索引,因为查询所需的所有列都在索引中。换句话说,查询只读取索引,而不是原始数据

请注意:如果字段是数字字段,则不需要在值周围加引号。使用单引号使它们看起来像字符串,这有时会混淆SQL优化器和阅读代码的人

编辑:

如注释中所述,添加索引的语法为:

create index idx_propcore_country_status ON propcore(corecountry, corestatus);

我通常使用表名和列名来命名索引(但名称可以是任何有效标识符)

那么,根据您所写的内容,我可以简单地执行以下操作:在propcore(corecountry,corestatus)上创建索引country_status吗?“对吗?”理查德。是的,这是正确的陈述。添加此索引后,我的解释现在给出:#id,选择类型,表,类型,可能的键,键,键长度,参考,行,额外的'1',简单','propcore',范围','country_status','country_status','4',NULL',27',使用where;使用索引“#id”,选择#type、table、type、可能的#key、key、key、len、ref、rows、额外的“1”、“SIMPLE”、“propcore”、“range”、“country#status”、“country#status”、“4”、NULL、“27”、使用where;使用索引'