sql - Row count based on table + index + statistics histogram -


i have nothing more table , column name.
based on that, i'm looking row count grouped given columnname. yes, run ...

select columnname, count_big(*) schema.tablename group columnname 

but means eating valuable resources. (io etc.)
wonder, can't not collect need statistics histogram?
after all, need narrow index on given column.
first of all, need collect name of narrow index.
task run query :

select st.name, si.name, si.index_id, sc.column_id sys.tables st     join sys.indexes si on si.[object_id] = st.[object_id]     join sys.index_columns sic on sic.[index_id] = si.[index_id] , sic.[object_id] = si.[object_id]     join sys.columns sc on sic.column_id = sc.column_id , sc.[object_id] = sic.[object_id] st.[object_id] = object_id('schemaname.tablename', 'u') , lower(sc.[name]) = 'columnname' 

this query gives me every index name, composite wide indexes. , that's not quite need.
might say, doesn't matter, since both index histograms contain exact row count attribute of column.
, you'd right.

here ugly problem.
statistics updated automatically when 20% of tables content changes. want numbers now. i'll have run update statistics. running update statistics on wide index takes time , eats resources meant save.
so, need 1 column narrow index. (yeay, first real use of narrow index! ) because takes no time @ update.

back query.
if query show me how many columns individual index holds, i'd set predicate "columncount = 1".

know add statement above, in order show columns contained within index?

why this?
need report showing how many rows in every staging table new , how many have been processed etl.
need report on daily basis.
column i'm looking have couple distinct values, histogram narrow index have max 5 steps.

i got it.

select schema_name(st.schema_id), st.name, si.[name], si.index_id, sc.column_id, ccnt.colcnt sys.tables st     join sys.indexes si on si.[object_id] = st.[object_id]     join sys.index_columns sic on sic.[index_id] = si.[index_id] , sic.[object_id] = si.[object_id]     join sys.columns sc on sic.column_id = sc.column_id , sc.[object_id] = sic.[object_id]     join (select isi.index_id, isi.[object_id], count_big(*) colcnt             sys.indexes isi                  join sys.index_columns isic on isic.[index_id] = isi.[index_id] , isic.[object_id] = isi.[object_id]                 join sys.columns isc on isic.column_id = isc.column_id , isc.[object_id] = isic.[object_id]             isic.is_included_column = 0             , isi.[object_id] = object_id('schemaname.tablename', 'u')             group isi.index_id, isi.[object_id]             ) ccnt on ccnt.index_id = si.index_id sic.is_included_column = 0 , lower(sc.[name]) = 'columname' , st.[object_id] = ccnt.[object_id] , ccnt.colcnt = 1 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -