I have managed to write the below query, which works great. My problem is that I am using it a bunch of times and I think it warrants its own view, but when I have phpmyadmin create the view for me, queries that used to take 0.0060 sec now take 6.2094 sec.
My Query:
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’s Conversion:
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`
Any ideas as to how I can make my view more time efficient?
ps: Here is the tags table structure:
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
Can’t really explain why your query got slower when converted into a view by phpMyAdmin, but I would try the following query instead: