如何优化MySQL表?

时间:2014-06-27 05:18:34

标签: mysql sql database performance indexing

我正在寻找一种提高表格性能的方法。下面是MySQL表。

+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| receiver | varchar(15) | NO   |     | NULL    |                |
| depCode  | varchar(12) | NO   |     | NULL    |                |
| sms      | text        | YES  |     | NULL    |                |
| billable | varchar(5)  | YES  |     | NULL    |                |
| smsc     | varchar(10) | YES  |     | NULL    |                |
| sendDate | date        | YES  |     | NULL    |                |
| sendTime | time        | YES  |     | NULL    |                |
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
+----------+-------------+------+-----+---------+----------------+

它有超过10,000,000条记录。

下面是我用来获取记录的SQL查询。 ${variable}是变量的语法。

SELECT count(*) as totalCount FROM `tbl_incoming` 
WHERE `depCode`=${depCode} AND `smsc`=${smsc_value} AND receivedDate BETWEEN  ${fromDate} AND ${toDate} 
ORDER BY receivedDate, receivedTime;

请参阅下面的EXPLAIN。

EXPLAIN SELECT * FROM `tbl_incoming`  
WHERE `depCode`="Slrd" AND `smsc`="dgsm" AND receivedDate  BETWEEN  "2009-11-26" AND "2014-11-26"  
ORDER BY receivedDate, receivedTime;

+----+-------------+--------------+------+---------------+------+---------+------+--------+-----------------------------+
| id | select_type | table        | type | possible_keys | key  | key_len | ref  | rows   | Extra                       |
+----+-------------+--------------+------+---------------+------+---------+------+--------+-----------------------------+
|  1 | SIMPLE      | tbl_incoming | ALL  | NULL          | NULL | NULL    | NULL | 542527 | Using where; Using filesort |
+----+-------------+--------------+------+---------------+------+---------+------+--------+-----------------------------+

根据我的知识,索引可以帮助提高表的性能。

PS:我正在寻找其他选项,除了索引。

如何改善表的性能?

1 个答案:

答案 0 :(得分:0)

索引应该可以帮到你。

在您的查询中,您在列depCode,smsc和receivedDate上有where条件。您必须对数据有更多的了解。 首先标识上面具有最多不同值的列3.将其选择为索引列,并将另外2添加到include中。

Eg:
Create Index IX_Custome On tbl_incoming (depCode) Include (smsc,receivedDate)

这只是一种方法,如果没用则忽略。