SQL 查看多个相同的表
SQL VIEW to the multiple same tables
我有 64 个 table 结构相同。我需要在每个 table 中找到行数,但我不想单独查询每个,所以我认为创建视图显然是...
CREATE VIEW globalResults AS
SELECT 'France' as country, count(RC) as complete FROM tableName1 where RC=18
UNION
SELECT 'UK' as country, count(RC) as complete FROM tableName2 where RC=18
UNION
SELECT 'Italy' as country, count(RC) as complete FROM tableName3 where RC=18
UNION
etc...
有没有更好的方法创建VIEW?
我想,你会想要查询一些对象。
此外,您将希望动态填充表。
在这种情况下,用户定义的函数内部有一个游标以循环遍历类似的表,这是在视图中使用静态硬编码表的解决方案的又一步
尽管性能可能很糟糕
在这里使用 view
没问题。
union all
效率更高,因为 union
会占用不必要的 distinct。
首先只需要第 header 列。
可以使用 count(*)
.
CREATE VIEW globalResults AS
SELECT 'UK' as country, count(*) as complete FROM tableName2 where RC = 18
union all
SELECT 'France', count(*) FROM tableName1 where RC = 18
union all
SELECT 'Italy', count(*) FROM tableName3 where RC = 18
...
您可以从 select name from sys.tables where type = 'u';
获得 table 个名字。
假设 table 的格式为 tablename1、tablename2、tablename3 等,您可以遍历每个 table一个循环并像这样获得所有计数:
create table #holding (country varchar(max), cnt int)
declare @iterator int = 1
while @iterator<=64 begin
exec('insert #holding select countryname, count(RC) FROM tableName'+@iterator+' where RC=18
group by countryname')
set @iterator=@iterator+1
end
select * from #holding
最好的解决方案可能是在每个 table 之间创建一个 VIEW
和 UNION ALL
的答案,这样您就可以按需查询(这就是我投票的地方)。如果您有一些国家名称到 table 名称的映射,动态生成该 VIEW 将非常容易。
如果您只需要 one-time 到 table 的行数,您可以使用统计信息。
SELECT
OS.name AS SchemaName,
O.name AS TableName,
SUM(S.row_count) AS RecordCount
FROM sys.dm_db_partition_stats S
INNER JOIN sys.objects O
ON o.object_id = s.object_id
INNER JOIN sys.schemas OS
ON OS.schema_Id = o.schema_id
WHERE S.index_id IN (0, 1) -- Ignore non-clustered indexes
AND O.type = 'U'
-- Additional filters for your tables of interest
GROUP BY OS.name, O.name
为了最大限度地提高准确性,也许 运行 DBCC UPDATEUSAGE (<myDB>) WITH COUNT_ROWS
以确保在 运行 之前刷新统计信息。
我有 64 个 table 结构相同。我需要在每个 table 中找到行数,但我不想单独查询每个,所以我认为创建视图显然是...
CREATE VIEW globalResults AS
SELECT 'France' as country, count(RC) as complete FROM tableName1 where RC=18
UNION
SELECT 'UK' as country, count(RC) as complete FROM tableName2 where RC=18
UNION
SELECT 'Italy' as country, count(RC) as complete FROM tableName3 where RC=18
UNION
etc...
有没有更好的方法创建VIEW?
我想,你会想要查询一些对象。 此外,您将希望动态填充表。
在这种情况下,用户定义的函数内部有一个游标以循环遍历类似的表,这是在视图中使用静态硬编码表的解决方案的又一步
尽管性能可能很糟糕
在这里使用 view
没问题。
union all
效率更高,因为 union
会占用不必要的 distinct。
首先只需要第 header 列。
可以使用 count(*)
.
CREATE VIEW globalResults AS
SELECT 'UK' as country, count(*) as complete FROM tableName2 where RC = 18
union all
SELECT 'France', count(*) FROM tableName1 where RC = 18
union all
SELECT 'Italy', count(*) FROM tableName3 where RC = 18
...
您可以从 select name from sys.tables where type = 'u';
获得 table 个名字。
假设 table 的格式为 tablename1、tablename2、tablename3 等,您可以遍历每个 table一个循环并像这样获得所有计数:
create table #holding (country varchar(max), cnt int)
declare @iterator int = 1
while @iterator<=64 begin
exec('insert #holding select countryname, count(RC) FROM tableName'+@iterator+' where RC=18
group by countryname')
set @iterator=@iterator+1
end
select * from #holding
最好的解决方案可能是在每个 table 之间创建一个 VIEW
和 UNION ALL
的答案,这样您就可以按需查询(这就是我投票的地方)。如果您有一些国家名称到 table 名称的映射,动态生成该 VIEW 将非常容易。
如果您只需要 one-time 到 table 的行数,您可以使用统计信息。
SELECT
OS.name AS SchemaName,
O.name AS TableName,
SUM(S.row_count) AS RecordCount
FROM sys.dm_db_partition_stats S
INNER JOIN sys.objects O
ON o.object_id = s.object_id
INNER JOIN sys.schemas OS
ON OS.schema_Id = o.schema_id
WHERE S.index_id IN (0, 1) -- Ignore non-clustered indexes
AND O.type = 'U'
-- Additional filters for your tables of interest
GROUP BY OS.name, O.name
为了最大限度地提高准确性,也许 运行 DBCC UPDATEUSAGE (<myDB>) WITH COUNT_ROWS
以确保在 运行 之前刷新统计信息。