Posts

Showing posts with the label Approx_Count_Distinct

SQL Server 2019 and the Approx_Count_Distinct function

Image
Hi Guys, Today we continue with the speech of the new features of SQL 2019, if last time we talked about Batch mode on Rowstore , today we talk about another new feature: the function Approx_Count_Distinct . Enjoy the reading, mate! The Approx_Count_Distinct function  Let's suppose for simplicity we take the same "movements" table used in the last post and already filled with 3 million rows. CREATE TABLE Moviments (ID INT IDENTITY(1,1), YEAR FLOAT , QTY FLOAT , PRICE FLOAT, CLASSIFICATORS VARCHAR(10)) Now suppose you want to know the number of items purchased for each classificators. Simple, you will say! just write this command: SELECT COUNT( DISTINCT CLASSIFICATORS) FROM MOVIMENTS True! However, it will not surprise you to know that this operation, which apparently seems to be so simple, instead requires many resources (and to read through all the rows of our table):   Table 'Worktable...

SQL Server 2019 e la funzione Approx_Count_Distinct

Image
Ciao a tutti! Per introdurre la novità di cui parleremo oggi partiamo da un esempio. Supponiamo abbiate una tabella dentro al vostro gestionale in cui vengano registrate tutte le vendite effettuate. Concettualmente le colonne di questa tabella saranno: il cliente che effettua l'acquisto, l'articolo che il cliente acquista, la quantità ed il prezzo. Supponiamo per semplicità che tale tabella abbia questa struttura: CREATE TABLE ELENCOVENDITE (ID INT IDENTITY(1,1), CLIENTE VARCHAR(80), ARTICOLO VARCHAR(80), QUANTITA FLOAT, PREZZO FLOAT) Supponiamo adesso che vogliate sapere il numero di articoli acquistati per ogni cliente. Semplice direte voi scrivendo il comando sotto: SELECT COUNT( DISTINCT (CLIENTE)) FROM ELENCOVENDITE Bravi risposta esatta! Non vi stupirà però sapere che questa operazione, che in apparenza sembra essere così semplice, richiede invece tante risorse. Richiede infatti di scorrere tutte le righe della nostra tabella. ...