Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query to select million records quickly

I want to select million records from a table and I am using select query for this.

Currently it is taking a few minutes to get data. Can I get it quickly?

I am using SQL Server 2008 R2.

My query:

SELECT     
   sum(Orders.BusinessVolumeTotal) as  BusinessVolume, 
   sum(Orders.CommissionableVolumeTotal) as CommissionableVolume, 
   OrderTypes.OrderTypeDescription, 
   Orders.OrderTypeID
FROM  
   Orders 
INNER JOIN
   OrderTypes ON Orders.OrderTypeID = OrderTypes.OrderTypeID
WHERE
   Orders.OrderDate > convert(DATETIME, '{0}') 
   and Orders.OrderDate < convert(DATETIME, '{1}') 
GROUP BY
   Orders.OrderTypeID, OrderTypes.OrderTypeDescription
like image 216
AnandMeena Avatar asked Dec 11 '22 10:12

AnandMeena


2 Answers

Use Indexing for your table fields for fetching data fast.

Reference:

http://www.tutorialspoint.com/sql/sql-indexes.htm

like image 188
Zeeshan Avatar answered Jan 10 '23 15:01

Zeeshan


There's a few factors that would go into this. A quick list of things to look at:

  1. The speed of your server. CPU, memory, network connection would all be factors
  2. If you are doing a SELECT statement with conditions (ie. using a WHERE) or one with JOINS, having indexes will improve your performance, especially on a table with millions of rows. Hash tables will do a huge net positive on a large table.
  3. Writing clean queries. For example, if you have a large list of items you need to exclude from a query, perform a LEFT JOIN instead of using a NOT IN condition.

This is really just the tip of the iceberg, but some of the easiest things to implement will also provide you with some of the biggest performance boosts.

like image 25
Lloyd Banks Avatar answered Jan 10 '23 15:01

Lloyd Banks