简体   繁体   English

SELECT COUNT(DISTINCT v))个性能

[英]SELECT COUNT(DISTINCT v)) performance

Simple question. 简单的问题。 How can I rewrite a query like this: 我该如何重写这样的查询:

SELECT a.name, MAX(b.value), MIN (b.value), COUNT(DISTINCT(b.value))
FROM tableA a
LEFT JOIN tableB b 
       ON a.type = b.type
WHERE b.value IS NOT NULL
GROUP BY a.name

So that it doesn't run dog-slow on a big-but-not-massive sized table? 这样它就不会在大型但不是大规模的桌子上慢下来吗? (let's say 1 million rows). (假设有100万行)。 Or would it be possible to do some other 'magic trick' on the database to make that query run quickly? 还是可以对数据库做一些其他“魔术”来使该查询快速运行?

Normalising the data further is out of the question in this particular case :) 在这种特殊情况下,进一步规范化数据是不可能的:)

Bit of additional information as requested 根据要求提供一些附加信息

Ideally, the solution would work for both MySQL and MS SQL Server 2008, although SQL Server is definitely the priority of those. 理想情况下,该解决方案对MySQL和MS SQL Server 2008均适用,尽管SQL Server绝对是其中的优先级。

The two tables should look like this: 这两个表应如下所示:

Table A:
    type INT NOT NULL PRIMARY KEY
    name VARCHAR(500

Table B:
    idTableC INT NOT NULL
    type INT NOT NULL
    value VARCHAR (50)

Table C:
    idTableC INT NOT NULL PRIMARY KEY
    ...

So generally, we want to go say: for each item in table C, get all items in table B with their type, specified in table A. 因此,通常来说,我们想说:对于表C中的每个项目,获取表B中所有具有其类型(在表A中指定)的项目。

However , it is also necessary to be able to say: for every 'type' in table A, get a summary of information associated with it in table B. It's this second case that this question's concerned with :) 但是 ,还必须能够说:对于表A中的每个“类型”,获得表B中与之相关的信息的摘要。此问题与这是第二种情况有关:)

You can use non-clustered indexes on foreign keys, I mean 2 index on [A.Type] and [B.Type] also you can have two other indexes on the columns in select [A.Name] , [B.value] 您可以在外键上使用非聚集索引,我的意思是[A.Type][B.Type]上有2个索引,在选择[A.Name][B.value]的列上也可以有两个其他索引

So that everything that your query needs is in an index. 这样您的查询所需的所有内容都在索引中。

Unsure which database you are using but you could ensure there is an index on the foreign key tableA.type and additionally an index on tableB.type which contains tableB.value. 不确定正在使用哪个数据库,但可以确保在外键tableA.type上有一个索引,并且在tableB.type上还有一个包含tableB.value的索引。 That way SQL won't need to go back to the data page to get the value and can simply retrieve it from the index. 这样,SQL无需返回到数据页即可获取值,而只需从索引中检索即可。 You should be careful with this as if it's a large value it could slow your index down. 您应该小心这一点,因为它的值太大会降低索引的速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM