我在页面上有超过30个搜索过滤器(我在Vitacost上读它的分面搜索,similar example)。在每个页面加载中,每个过滤器都会进行查询(现在有大约50个查询与其他类型的查询),以根据用户的搜索结果更新过滤器选项。我有类似SELECT DISTINCT
这样的查询
SELECT DISTINCT(Category) FROM `Toy`
WHERE (Material LIKE '%Steel%')
AND Material <> ''
ORDER BY Material
SELECT DISTINCT(Manufacturer) FROM `Toy`
WHERE (Material LIKE '%Steel%')
AND Manufacturer <> ''
ORDER BY Manufacturer
SELECT DISTINCT(Rating) FROM `Toy`
WHERE (Material LIKE '%Steel%')
AND Rating <> ''
ORDER BY Rating
我想知道是否有任何提升性能或至少减少查询的空间。我已经尝试了JOIN
,但没有看到太多改进,结果是错误的。有没有人有任何想法或建议?
SELECT *
FROM (
SELECT DISTINCT(Category),Material FROM `Toy`
WHERE (Material LIKE '%Steel%')
AND Material <> ''
ORDER BY Material
)TAB1
JOIN (
SELECT DISTINCT(Manufacturer),Material FROM `Toy`
WHERE (Material LIKE '%Steel%')
AND Manufacturer <> ''
ORDER BY Manufacturer
)TAB2 ON TAB1.Material = TAB2.Material
JOIN (
SELECT DISTINCT(Rating),Material FROM `Toy`
WHERE (Material LIKE '%Steel%')
AND Rating <> ''
ORDER BY Rating
)TAB3 ON TAB2.Material = TAB3.Material
GROUP BY TAB2.Material
表:
CREATE TABLE Toy
(`Toyid` int, `Toy` varchar(20),`Category` varchar(30),`Manufacturer` varchar(3),`Rating` varchar(10),`Material` varchar(20))
;
INSERT INTO Toy
(`Toyid`,`Toy`,`Category`,`Manufacturer`,`Rating`,`Material`)
VALUES
(1, 'ToyA','Outdoor','AAA','5','Plastic'),
(2, 'ToyB','Doll','AAA','5','Gold'),
(3, 'ToyC','Brainteaser','AAA','4','Metal'),
(4, 'ToyD','Educational','CCC','5','PVD Steel'),
(5, 'ToyE','Outdoor','DDD','3','Plastic'),
(6, 'ToyF','Figure','EEE','3','Steel'),
(7, 'ToyG','Creative','TTT','2','Stainless Steel'),
(8, 'ToyH','Doll','DDD','2','Steel'),
(9, 'ToyI','Outdoor','TTT','5','Stainless Steel'),
(10, 'ToyJ','Brainteaser','DDD','2','PVD GOLD'),
(11, 'ToyK','Creative','CCC','4','Rose Gold'),
(12, 'ToyL','Figure','KKK','2','Gold')
;
我的预期输出:
CATEGORY
Educational
Creative
Outdoor
Figure
Doll
RATING
2
3
5
MANUFACTURER
CCC
DDD
EEE
TTT
所以我想找到一个解决方案,将它们连接在一个输出中。
答案 0 :(得分:1)
您可以尝试这样的事情: -
SELECT DISTINCT Category, Material, Manufacturer, Rating
FROM Toy
WHERE Material LIKE '%Steel%'
AND Material <> ''
AND Manufacturer <> ''
AND Rating <> ''
ORDER BY Material, Manufacturer, Rating;
这可能对您有所帮助。
答案 1 :(得分:1)
这是您在sqlfidle上查询(使用加入)的结果:
| CATEGORY | MATERIAL | MANUFACTURER | RATING |
|-------------|-----------------|--------------|--------|
| Educational | PVD Steel | CCC | 5 |
| Creative | Stainless Steel | TTT | 2 |
| Doll | Steel | DDD | 2 |
但是如果你单独运行每个查询
| CATEGORY |
|-------------|
| Educational |
| Creative |
| Outdoor |
| Doll |
| Figure |
| MANUFACTURER |
|--------------|
| CCC |
| DDD |
| EEE |
| TTT |
| RATING |
|--------|
| 2 |
| 3 |
| 5 |
类别图或室外?或评级3?或制造商EEE?在你的综合查询?合并后的查询是错误的,也不会提供性能提升。
您应该通过上述建议获得一些性能提升,但请查看结果:
| MATERIAL | CATEGORY | MANUFACTURER | RATING |
|-----------------|-------------|--------------|--------|
| Stainless Steel | Creative | TTT | 2 |
| Steel | Doll | DDD | 2 |
| PVD Steel | Educational | CCC | 5 |
| Steel | Figure | EEE | 3 |
| Stainless Steel | Outdoor | TTT | 5 |
虽然这些是“不同的”,但它们在整个行中是截然不同的。所以没有专栏 - 本身 - 是一个独特的列表。因此,例如,如果使用PHP,则需要将此多列结果收集到数组中,然后为每个下拉列表创建单独的不同数组(可能使用array_unique)。这样可以减少DBMS的工作量,但需要更多的PHP工作量。
请注意distinct
不是函数,它实际上是关键字选择的条件,我建议您在不同之后立即停止使用括号:SELECT DISTINCT(Category)
应该只是SELECT DISTINCT Category
}
一般情况下,在真正需要时你应该ORDER BY
,如果你可以避免“双端”通配符搜索,这也有助于提高性能。