MariaDB - 我应该为我的 table 添加索引吗?
MariaDB - Should I add index to my table?
最近我在检查我的系统日志,我发现我的一些查询非常慢。
我有一个存储用户活动的 table。 table 结构是 id (int), user (int), type (int), object (varchar), extra (mediumtext) and date (timestamp)
.
另外我只有 id (BTREE, unique)
的索引。
我遇到以下查询的性能问题;
SELECT DISTINCT object as usrobj
from ".MV15_PREFIX."useractivities
WHERE user='".$user_id."'
and type = '3'
limit 0,1000000"
问题是,我是否也应该索引 user
和 id
一样?我应该遵循的最佳做法是什么?
这个 table 被积极使用,其中有超过 500k+ 行。现场平均同时在线人数2k~
我问这个问题的原因是我不太擅长管理数据库,而且我在另一个具有适当索引的 table 上有慢查询问题。
提前感谢您的建议。
旁注:
mysqltuner 的结果
一般建议:
Reduce or eliminate persistent connections to reduce connection usage
Adjust your join queries to always utilize indexes
Temporary table size is already large - reduce result set size
Reduce your SELECT DISTINCT queries without LIMIT clauses
Consider installing Sys schema from https://github.com/mysql/mysql-sys
要调整的变量:
max_connections (> 768)
wait_timeout (< 28800)
interactive_timeout (< 28800)
join_buffer_size (> 64.0M, or always use indexes with joins)
(我将设置 max_connections
> 768,不太确定 超时 并且据我在 Whosebug 中阅读 topics/suggestions 我认为我不应该增加 join_buffer_size
的大小,但我也非常希望能得到有关这些变量的反馈。)
编辑 - 显示索引结果;
+--------------------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| ***_useractivities | 0 | PRIMARY | 1 | id | A | 434006 | NULL | NULL | | BTREE | | |
| ***_useractivities | 1 | user_index | 1 | user | A | 13151 | NULL | NULL | | BTREE | | |
| ***_useractivities | 1 | user_type_index | 1 | user | A | 10585 | NULL | NULL | | BTREE | | |
| ***_useractivities | 1 | user_type_index | 2 | type | A | 13562 | NULL | NULL | | BTREE | | |
+--------------------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
大多数 PostgreSQL 索引经验法则适用于大多数 SQL 数据库管理系统。
https://dba.stackexchange.com/a/31517/1064
所以,是的,您可能会受益于 user
上的索引和 type
上的索引。您可能会从 user, type
.
对的索引中获益更多
你也会从学习如何read an execution plan中获益。
对于那个查询,其中是最优的:
INDEX(user, type)
INDEX(type, user)
单独的索引 (INDEX(user), INDEX(type)
) 可能没有那么好。
MySQL的InnoDB只有BTree
,没有Hash
。无论如何,对于 'point queries',BTree 本质上与 Hash 一样好,并且对于 'range' 查询要好得多。
索引tips.
索引对 SELECTs
和 UPDATEs
的帮助有时很大。使用它们。副作用很小——例如使用了额外的磁盘space。
最近我在检查我的系统日志,我发现我的一些查询非常慢。
我有一个存储用户活动的 table。 table 结构是 id (int), user (int), type (int), object (varchar), extra (mediumtext) and date (timestamp)
.
另外我只有 id (BTREE, unique)
的索引。
我遇到以下查询的性能问题;
SELECT DISTINCT object as usrobj
from ".MV15_PREFIX."useractivities
WHERE user='".$user_id."'
and type = '3'
limit 0,1000000"
问题是,我是否也应该索引 user
和 id
一样?我应该遵循的最佳做法是什么?
这个 table 被积极使用,其中有超过 500k+ 行。现场平均同时在线人数2k~
我问这个问题的原因是我不太擅长管理数据库,而且我在另一个具有适当索引的 table 上有慢查询问题。
提前感谢您的建议。
旁注:
mysqltuner 的结果
一般建议:
Reduce or eliminate persistent connections to reduce connection usage
Adjust your join queries to always utilize indexes
Temporary table size is already large - reduce result set size
Reduce your SELECT DISTINCT queries without LIMIT clauses
Consider installing Sys schema from https://github.com/mysql/mysql-sys
要调整的变量:
max_connections (> 768)
wait_timeout (< 28800)
interactive_timeout (< 28800)
join_buffer_size (> 64.0M, or always use indexes with joins)
(我将设置 max_connections
> 768,不太确定 超时 并且据我在 Whosebug 中阅读 topics/suggestions 我认为我不应该增加 join_buffer_size
的大小,但我也非常希望能得到有关这些变量的反馈。)
编辑 - 显示索引结果;
+--------------------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| ***_useractivities | 0 | PRIMARY | 1 | id | A | 434006 | NULL | NULL | | BTREE | | |
| ***_useractivities | 1 | user_index | 1 | user | A | 13151 | NULL | NULL | | BTREE | | |
| ***_useractivities | 1 | user_type_index | 1 | user | A | 10585 | NULL | NULL | | BTREE | | |
| ***_useractivities | 1 | user_type_index | 2 | type | A | 13562 | NULL | NULL | | BTREE | | |
+--------------------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
大多数 PostgreSQL 索引经验法则适用于大多数 SQL 数据库管理系统。
https://dba.stackexchange.com/a/31517/1064
所以,是的,您可能会受益于 user
上的索引和 type
上的索引。您可能会从 user, type
.
你也会从学习如何read an execution plan中获益。
对于那个查询,其中是最优的:
INDEX(user, type)
INDEX(type, user)
单独的索引 (INDEX(user), INDEX(type)
) 可能没有那么好。
MySQL的InnoDB只有BTree
,没有Hash
。无论如何,对于 'point queries',BTree 本质上与 Hash 一样好,并且对于 'range' 查询要好得多。
索引tips.
索引对 SELECTs
和 UPDATEs
的帮助有时很大。使用它们。副作用很小——例如使用了额外的磁盘space。