简体   繁体   English

索引mysql数据库中的所有列是否正确?

[英]Is it correct to index all columns in a mysql database?

My site is taking too long to load the contents and the CPU load used by mysql service reaches sometimes 200%. 我的网站加载内容的时间太长,mysql服务使用的CPU负载有时达到200%。 Checking the tables realized that the tables all columns were used as index. 检查表意识到表的所有列都用作索引。 This is correct? 这是对的? It can affect performance? 它会影响性能吗?

My server configuration: 16GB RAM, 3.4GHz Most tables have about 25k ~ 50k lines. 我的服务器配置:16GB RAM,3.4GHz大多数表有大约25k~50k线。 And 10~20 Columns. 和10~20列。

All tables indexing are like this example. 所有表索引都像这个例子。 All columns as indexes 所有列都作为索引

No, you don't index all columns. 不,您不索引所有列。 You index columns that are specifically involved in a WHERE clause, and sometimes if they're involved in an ORDER BY . 您索引WHERE子句中专门涉及的列,有时如果它们涉及ORDER BY

In this case you'd want an index on type : 在这种情况下,您需要一个type索引:

SELECT name FROM users WHERE type='admin'

In this case you'd want an index on active,type : 在这种情况下,您需要一个active,type索引active,type

SELECT name FROM users WHERE type='admin' AND active=1

In this case you might want an index on active,type,name : 在这种情况下,您可能需要有关active,type,name的索引:

SELECT name FROM users WHERE type-='admin' AND active=1 ORDER BY name LIMIT 10

The more indexes you add the slower writes will be but the faster reads will be. 您添加的索引越多,写入速度就越慢,但读取速度会越快。 This is a classic trade-off. 这是一个经典的权衡。 Evaluate carefully what indexes you need and apply them only if there will be a tangible benefit. 仔细评估您需要哪些索引,并仅在有实际利益的情况下应用它们。 Don't just slap them on because you feel like they should be there. 不要只是打他们,因为你觉得他们应该在那里。

On super tiny tables, those with <1000 rows, indexes won't help that much because a table scan won't take that long. 在超小型表上,那些<1000行,索引的表将无济于事,因为表扫描不会花那么长时间。 On anything non-trivial they're absolutely essential. 在任何不重要的事情上,他们绝对必不可少。

If you're having performance problems I'd suggest that your schema is the biggest obstacle, not the lack of indexes. 如果您遇到性能问题,我建议您的架构是最大的障碍,而不是缺少索引。

Indexing all columns is not correct. 索引所有列是不正确的。 It affects performance of write operations. 它会影响写入操作的性能。 Each additional index requires additional time to update after write operation. 每个附加索引在写入操作后需要额外的时间来更新。 Also each index costs additional space. 每个索引也需要额外的空间。 You should index only the columns which will be used for searching. 您应该仅索引将用于搜索的列。

No, you should not index all columns. 不,您不应该索引所有列。 It will be slower when you write data. 写数据时速度会慢一些。 It is not normal that your CPU reaches sometimes 200%. 你的CPU有时达到200%是不正常的。 I suggest you check your SQL. 我建议你查一下你的SQL。

I'm sure optimizing SELECT statements is helpful. 我确信优化SELECT语句很有帮助。

http://dev.mysql.com/doc/refman/5.6/en/statement-optimization.html http://dev.mysql.com/doc/refman/5.6/en/statement-optimization.html

1.Optimize your query with cache. 1.使用缓存优化您的查询。 Try to use parameter instead of functions. 尝试使用参数而不是函数。

(Thanks for @tadman's suggestion) (感谢@ tadman的建议)

// no cache
$sql = "SELECT username FROM user WHERE signup_date >= CURDATE()";

// cache
$today = date("Y-m-d");
$sql = "SELECT username FROM user WHERE signup_date >= '$today'"); 

2.EXPLAIN you SELECT wisely. 2.明智地选择你的选择。 It helps you find potential performance issues. 它可以帮助您找到潜在的性能问题。

EXPLAIN select name,phone from user where name="JakLiao";

3. Use "LIMIT N" when you only need first one row result. 3.当您只需要第一行结果时,使用“LIMIT N”。

SELECT * FROM user WHERE country = 'China' limit 1;

4. Use index when you need it in where clause. 4.在where子句中需要时使用索引。

//row "name" should add index.
select * from user where name LIKE 'Jak%' limit 10;

5. Use index when you join tables. 5.连接表时使用索引。

select company_name FROM users LEFT JOIN companies ON (users.state = companies.state) WHERE users.id = 123;

6. Avoid "select *" 6.避免“选择*”

//not suggest
SELECT * FROM user WHERE user_id = 1;
//suggest
SELECT username FROM user WHERE user_id = 1;

7. PROCEDURE ANALYSE(), MYSQL will help you analysis your database and data. 7. PROCEDURE ANALYZE(),MYSQL将帮助您分析数据库和数据。 8. COUNT(1) when you want to know rows of a table. 8.当您想知道表的行时,COUNT(1)。

//not suggest
SELECT count(*) FROM user;
//suggest
SELECT count(1) FROM user;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM