问题
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:
- The speed of your server. CPU, memory, network connection would all be factors
- 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.
- 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:
- Indexing Clustered/Non clustered
- Data Caching
- Table Partitioning
- Execution Plan caching
- 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