我设法编写了以下查询,效果很好。我的问题是我多次使用它,我认为它有自己的观点,但是当我让 phpmyadmin 为我创建视图时,过去需要 0.0060 秒的查询现在需要 6.2094 秒。
我的查询:
SELECT tr.uuid, tr.creator,
(
SELECT
GROUP_CONCAT(name)
FROM tags as t1
WHERE t1.uuid = tr.uuid and t1.type = "name"
GROUP BY t1.uuid
) as names,
(
SELECT GROUP_CONCAT(name)
FROM tags as t2
WHERE t2.uuid = tr.uuid and t2.type = "tag"
GROUP BY t2.uuid
) as tags
FROM `tags` as tr
phpMyAdmin 的转换:
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost`
SQL SECURITY DEFINER
VIEW `textagname` AS
select
`tr`.`uuid` AS `uuid`,
`tr`.`creator` AS `creator`,
(
select group_concat(`t1`.`name` separator ',') AS `GROUP_CONCAT(name)`
from `tags` `t1`
where ((`t1`.`uuid` = `tr`.`uuid`) and (`t1`.`type` = 'name'))
group by `t1`.`uuid`
) AS `names`,
(
select group_concat(`t2`.`name` separator ',') AS `GROUP_CONCAT(name)`
from `tags` `t2`
where ((`t2`.`uuid` = `tr`.`uuid`) and (`t2`.`type` = 'tag'))
group by `t2`.`uuid`
) AS `tags`
from `tags` `tr`
关于如何让我的视图更省时的任何想法?
ps:tags
表结构如下:
Column Type Null Default Comments
------- ----------- ---- ------- ------------------
uuid varchar(36) No key of texture
name varchar(64) No tag name
creator varchar(36) Yes NULL creator of the tag
type varchar(36) No name, or tag