我的查询速度有问题 - 目前它的运行时间约为 16 秒,我需要加快速度!
我的表方案如下:
用户:
- id (int 10, 主键)
- 用户名(varchar 100)
- 密码(varchar 100)
用户元:
- id (int 10, 主键)
- 用户(int 10)
- 元(varchar 100)
- 值(长文本)
我需要从用户元表中的各个行(例如名字、姓氏等)作为列返回数据。此查询完成了这项工作,但运行速度太慢:
SELECT
Users.id as id,
Users.username as username,
firstName.value as metaFirstName,
lastName.value as metaLastName,
userLevel.value as metaUsername,
employer.value as metaEmployer,
gto.value as metaGTO
FROM Users
LEFT JOIN (Users_meta as firstName) ON (firstName.user = Users.id AND firstName.meta = 'first_name')
LEFT JOIN (Users_meta as lastName) ON (lastName.user = Users.id AND lastName.meta = 'last_name')
LEFT JOIN (Users_meta as userLevel) ON (userLevel.user = Users.id AND userLevel.meta = 'user_level')
LEFT JOIN (Users_meta as employer) ON (employer.user = Users.id AND employer.meta = 'employer')
LEFT JOIN (Users_meta as gto) ON (gto.user = Users.id AND gto.meta = 'gto')
我还需要能够将 WHERE 和 ORDER BY 子句添加到查询中。
谢谢你的帮助。:)