|
一共三张表tips,tippings,comments。tippings 是中间表。tips 和 comments 是多对多的关系。tips只有两列:id,name
tippings 的create语句:
- CREATE TABLE `tippings` (
- `id` INT(11) NOT NULL AUTO_INCREMENT,
- `tip_id` INT(11) NULL DEFAULT NULL,
- `tippable_id` INT(11) NULL DEFAULT NULL,
- `tipper_id` INT(11) NULL DEFAULT NULL,
- `tipper_type` VARCHAR(255) NULL DEFAULT NULL,
- `tippable_type` ENUM('Post','Comment') NULL DEFAULT NULL,
- `context` ENUM('tips') NULL DEFAULT NULL,
- `created_at` DATETIME NULL DEFAULT NULL,
- PRIMARY KEY (`id`),
- INDEX `index_tippings_on_tip_id` (`tip_id`),
- INDEX `index_tippings_on_tippable_id_and_tippable_type_and_tip_id` (`tippable_id`, `tippable_type`, `tip_id`),
- INDEX `index_cttt` (`context`, `tippable_type`, `tippable_id`, `tip_id`)
- )
复制代码
查询语句为:- SELECT tips.*, COUNT(*) AS count FROM `tips` LEFT OUTER JOIN tippings
- ON tips.id = tippings.tip_id INNER JOIN
- comments ON comments.id = tippings.tippable_id WHERE (tippings.tippable_type
- = 'Comment') GROUP BY tips.id HAVING COUNT(*) > 0 ORDER BY
- count desc LIMIT 75;
复制代码 tippings 目前800多万条记录。
查询速度太慢
怎么设置索引能使按那个聚合函数count(*) 排序时不产生 Using temporary,当然更改my.cnf当中tmp_table_size的大小先除外考虑。
各位帮忙看看能不能更好的优化下
|
|