Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All

I have the following table without any primary key and Index. This table contains more than 20 Million records. Hence the Select statement is very slow (more than 20 sec for a date range 2012-12-01 to 2012-12-31).

SQL
CREATE TABLE tblDataLog(
    [DATE_TIME] [datetime] NOT NULL,
    [5] [float] NULL,
    [6] [float] NULL,
    [7] [float] NULL,
    [8] [float] NULL,
    [9] [float] NULL,
    [10] [float] NULL,
    [11] [float] NULL,
    [12] [float] NULL,
    [13] [float] NULL,
    [14] [float] NULL,
    [15] [float] NULL,
    [16] [float] NULL,
    [17] [float] NULL,
    [18] [float] NULL,
    [19] [float] NULL,
    [20] [float] NULL,
    [21] [float] NULL,
    [22] [float] NULL,
    [23] [float] NULL,
    [24] [float] NULL,
    [25] [float] NULL,
    [26] [float] NULL,
    [27] [float] NULL,
    [28] [float] NULL,
    [29] [float] NULL,
    [30] [float] NULL,
    [31] [float] NULL,
    [32] [float] NULL,
    [33] [float] NULL,
    [34] [float] NULL,
    [35] [float] NULL,
    [36] [float] NULL,
    [37] [float] NULL,
    [38] [float] NULL,
    [39] [float] NULL,
    [40] [float] NULL,
    [41] [float] NULL
) ON [PRIMARY]


Please suggest me how to optimize this table.

Thanks.
Willington
Posted

Hi All,

Thanks for reply. I have solved the issue by creating NonClustered index on Date_Time Column and included the columns which are frequently used in the select clause.

SQL
CREATE INDEX NDX_tblTestData
     ON tblTestData
        (DATE_TIME) INCLUDE (Col1, Col2, Col7);
 
Share this answer
 
you should add auto-incr primary key
and then apply indexing

refer this link having same problem you are facing

http://stackoverflow.com/questions/1716798/sql-server-2008-ordering-by-datetime-is-too-slow[^]

Happy Coding!
:)
 
Share this answer
 
Hi,

Keep an ID column as primary key and use indexing on any two columns for retrieving the records.
See the following link for indexing.

CREATE INDEX (Transact-SQL)
Overview of SQL Server 2005/2008 Table Indexing
SQL Server Journey with SQL Authority

Thanks
 
Share this answer
 
Comments
willington.d 4-Jan-13 3:43am    
Hi, I have added ID as primarykey column and Created NonClustered(Date_Time) Index on the table. Now the issue is, it is very slow to execute the following Query

SELECT DATE_TIME, [794] AS CT, [91] AS BID,
ROW_NUMBER() OVER (PARTITION BY [91] ORDER BY [794] DESC) AS Row_No
FROM tblDataLog (NOLOCK)
WHERE DATE_TIME >= '2012-12-01 12:00:00' and DATE_TIME <= '2012-12-31 11:59:59'

it is taking more than 30sec.

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