我有一个像comment
这样的表格,其行数为100k +,并且每天都在增加
我经常需要从此表中选择记录
+----+---------+-------
| id | comment | type |
+----+---------+------+
| 1 aaa photo|
| 2 bbb phono|
| 3 ccc video|
| 4 ddd video|
| 5 eee story|
... ... ...
如果创建除1个表之外的3个表,它会更快吗?
as
comment_photo
+----+---------+
| id | comment |
+----+---------+
| 1 aaa |
| 2 bbb |
... ...
comment_video
+----+---------+
| id | comment |
+----+---------+
| 1 ccc |
| 2 ddd |
... ...
comment_story
+----+---------+
| id | comment |
+----+---------+
| 1 eee |
| 2 fff |
... ...
答案 0 :(得分:1)
制作表格comment_photo
,comment_video
并不是一个好主意。如果您将来还有其他类型怎么办?下一张桌子?
更好的方法是添加表格comment_type
:
+----+--------+
| id | name |
+----+--------+
| 1 photo |
| 2 video |
| 3 story |
... ...
然后将comment
更改为:
+----+---------+---------
| id | comment | type_id |
+----+---------+---------+
| 1 aaa 1 |
| 2 bbb 1 |
| 3 ccc 2 |
| 4 ddd 2 |
| 5 eee 3 |
... ... ...
还要记住计划好的索引。尝试EXPLAIN您的查询并记住 - 嵌套循环是您的敌人。