sql query to select millions record very fast

為{幸葍}努か 提交于 2019-12-03 10:20:28

问题


I want to select millions record from a table and I am using select query for this.

Currently it is taking few minutes to get data.

Can I get it quickly.

I am using SQL Server 2008 R2.

I am using below 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

Thanks in advance.


回答1:


Use Indexing for your table fields for fetching data fast.

Reference:

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




回答2:


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.




回答3:


The speed of the query depends on the number of rows but if you do appropriate optimizations taking the performance factors such as:

  1. Indexing Clustered/Non clustered
  2. Data Caching
  3. Table Partitioning
  4. Execution Plan caching
  5. Data Distribution

the query will execute faster.




回答4:


Best way Fast Performance Tips for SQL Usiing SELECT Statements

   1:- Check Indexes. 
   2:- There should be indexes on all fields used in the WHERE and JOIN portions of the SQL statement
   3:-  Limit Size of Your Working Data Set.
   4:-  Only Select Fields You select as Need.
   5:- Remove Unnecessary Table and index 
   6:- Remove OUTER JOINS. 
   7:-  Remove Calculated Fields in JOIN and WHERE Clauses.


来源:https://stackoverflow.com/questions/19511249/sql-query-to-select-millions-record-very-fast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!