说我有这样一张桌子:
Field1 Field2 Field3 Field4
fred tom fred harry
tom tom
dick harry
harry
我想确定每个字段的完成比例。
我可以执行:
SELECT COUNT (Field1) WHERE (Field1 <> '') AS Field1Count
SELECT COUNT (Field2) WHERE (Field2 <> '') AS Field2Count
SELECT COUNT (Field3) WHERE (Field3 <> '') AS Field3Count
SELECT COUNT (Field4) WHERE (Field4 <> '') AS Field4Count
是否可以将这些单独的SQL语句汇总成一个将在一次命中中返回4个结果的SQL语句?这样做是否有任何性能优势(假设在实践中列和行的数量可能非常大)?
答案 0 :(得分:5)
你可以这样做:
select
sum(case when Field1 <> '' then 1 else 0 end) as Field1Count,
sum(case when Field2 <> '' then 1 else 0 end) as Field2Count,
sum(case when Field3 <> '' then 1 else 0 end) as Field3Count,
sum(case when Field4 <> '' then 1 else 0 end) as Field4Count
from TheTable
答案 1 :(得分:2)
如果将未填充的字段设置为NULL而不是空白,则可以依赖于count()
不包含NULL字段的事实。具有每行功能的所有解决方案(if
,case
,coalesce
等等)适用于小型数据库,但不能很好地扩展到大型数据库。请记住,small是一个相对术语,即使您认为它们很大,它对您的数据库仍然可以 - 我在一个数百万行是我们的配置表大小的商店工作: - )
然后你可以使用:
select
count(field1) as count1,
count(field2) as count2,
count(field3) as count3,
count(field4) as count4
from ...
(或count(distinct fieldX)
表示不同的值。)
如果这是一种似是而非的方法,你可以设置你的表:
update tbl set field1 = NULL where field1 = '';
update tbl set field2 = NULL where field2 = '';
update tbl set field3 = NULL where field3 = '';
update tbl set field4 = NULL where field4 = '';
但是,与所有数据库性能问题一样,测量,不要猜测。并在目标环境(或合适的副本)中进行测量。经常测量。数据库调优不是一种“一劳永逸”的操作。
答案 2 :(得分:1)
以下是我将如何使用MySQL
select sum(CASE WHEN Field1 <>'' THEN 1 ELSE 0 END) as Field1Count
, sum(CASE WHEN Field2 <>'' THEN 1 ELSE 0 END) as Field2Count
, sum(CASE WHEN Field3 <>'' THEN 1 ELSE 0 END) as Field3Count
...
, sum(CASE WHEN FieldN <>'' THEN 1 ELSE 0 END as FieldNCount
from DataTable