Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi frenz, I have a problem of slow loading of distinct column values from table.Can i know the alternative way to load the data from database much faster. The query below is taking 1 and half minute to fetch data. Please suggest the right way. Thank you.

What I have tried:

SET @t1 = CONCAT('SELECT DISTINCT gender FROM ', tablename);
SET @t2 = CONCAT('SELECT DISTINCT country FROM ',tablename);
SET @t3 = CONCAT('SELECT DISTINCT DATE_FORMAT(date_time,\'%Y-%m-%d\') as date_time FROM ', tablename,' where source=','\'',sourceslist, '\'');
SET @t4 = CONCAT('SELECT DISTINCT product_name FROM ', tablename);
SET @t5 = CONCAT('SELECT DISTINCT source FROM ', tablename);

here table name takes the dynamic name which is containing more than 3800000 rows
Posted
Updated 17-May-16 21:32pm

1 solution

Create single query with all the values
SELECT dISTINCT gender, country, dateformat, product_name, source FROM table

Then create @tx by using returned values like this:
@t1 = returnedTable.DefaultView.ToTable(true, "gender");


Better solution would be to have separate tables gender, countries, date_formats, products and sources which then would be linked to the main table and you could get 2, 10 or 50 values without having to retrieve 4 million rows multiple times.


If you absolutely have to have that huge table, at least create indexes on the fields you're retrieving...but whatever you do, you will never have good performance based on bad design.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900