Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select 1000 distinct names from 100 million records via standard sql

I have a table tb_FirstName with one field FirstName. The table has 100 million non null records with lots of repetitions e.g. John occurs 2 million times. The distinct count of FirstName is over 2 million.

How do I select 1000 distinct names as quickly as possible using standard sql?

I'm currently using the following but this is

  • tSQL
  • Maybe not as efficient as it could be.

    SELECT x.FirstName
    FROM (
        SELECT  FirstName,
                rnk = RANK() OVER (ORDER BY Firstname)
        FROM    WHData.dbo.tb_DimUserAccount A
        GROUP BY FirstName
        ) x
    WHERE rnk <=1000
    
like image 853
whytheq Avatar asked Mar 29 '13 11:03

whytheq


People also ask

How do I SELECT distinct column names in SQL?

To get unique or distinct values of a column in MySQL Table, use the following SQL Query. SELECT DISTINCT(column_name) FROM your_table_name; You can select distinct values for one or more columns. The column names has to be separated with comma.

How can I get distinct values from multiple columns in SQL?

In SQL multiple fields may also be added with DISTINCT clause. DISTINCT will eliminate those rows where all the selected fields are identical.


2 Answers

Seems like you could use TOP 1000 with DISTINCT:

SELECT DISINCT TOP 1000 FirstName
FROM WHData.dbo.tb_DimUserAccount
ORDER BY FirstName

Condensed SQL Fiddle Demo

like image 75
sgeddes Avatar answered Sep 22 '22 09:09

sgeddes


Try this

SELECT TOP 1000 FirstName FROM 
(SELECT 
ROW_NUMBER() OVER(PARTITION BY FirstName ORDER BY FirstName) NO,
 FirstName FROM WHData.dbo.tb_DimUserAccount )
  AS T1 WHERE no =1 

or

SELECT DISINCT TOP 1000 FirstName
FROM WHData.dbo.tb_DimUserAccount ORDER BY FirstName
like image 37
Harshil Avatar answered Sep 22 '22 09:09

Harshil