Improve SQL Server query performance with table having medium data - sql-server

I have a table with 6000 records but it could be larger in the future. I have attached screen dump of my current SQL query which takes 12 to 14 seconds to load 6000 rows of data.
There are already indexes on the columns which are queried most commonly
How can I improve query performance or modify query for the same?
CREATE TABLE [dbo].[Item]
(
[Srno] [BIGINT] IDENTITY(1, 1) NOT NULL,
[ITEMCode] [BIGINT] NOT NULL,
[BranchId] [BIGINT] NOT NULL,
[ITEM_No] [VARCHAR](50) NULL,
[ITEM_Desc] [VARCHAR](200) NULL,
[DeptId] [BIGINT] NULL,
[CatId] [BIGINT] NULL,
[SizeId] [BIGINT] NULL,
[CostPrice] [DECIMAL](18, 3) NULL,
[SalesPrice] [DECIMAL](18, 2) NULL,
[ITEM_InStock] [BIGINT] NULL,
[UserId] [BIGINT] NULL,
[Active] [BIT] NOT NULL,
[IsDeleted] [BIT] NOT NULL,
[Quantity] [INT] NULL,
[IsFavourite] [BIT] NULL,
[IsProductLink] [BIT] NULL,
CONSTRAINT [PK_Item_1] PRIMARY KEY CLUSTERED ( [ITEMCode] ASC, [BranchId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
ON [PRIMARY]
ALTER TABLE [dbo].[Item]
WITH CHECK ADD CONSTRAINT [FK_Item_Item] FOREIGN KEY([ITEMCode], [BranchId]) REFERENCES [dbo].[Item] ([ITEMCode], [BranchId])
GO
ALTER TABLE [dbo].[Item]
CHECK CONSTRAINT [FK_Item_Item]
GO
ALTER TABLE [dbo].[Item]
ADD CONSTRAINT [DF_Item_ITEM_UnitPrice] DEFAULT ((0)) FOR [CostPrice]
GO
ALTER TABLE [dbo].[Item]
ADD CONSTRAINT [DF_Item_ITEM_RETAILPRICE] DEFAULT ((0)) FOR [SalesPrice]
GO
ALTER TABLE [dbo].[Item]
ADD CONSTRAINT [DF_Item_ITEM_InStock] DEFAULT ((0)) FOR [ITEM_InStock]
GO
ALTER TABLE [dbo].[Item]
ADD CONSTRAINT [DF_Item_Active] DEFAULT ((0)) FOR [Active]
GO
ALTER TABLE [dbo].[Item]
ADD CONSTRAINT [DF_Item_DelFlag] DEFAULT ((0)) FOR [IsDeleted]
GO
I have updated query and removed join of Department table but still it takes 10 Seconds
SELECT Item.ITEMCode AS ItemId,
ISNULL(Item.ITEM_No, '0') AS ItemNo,
isnull(Item.Barcode, '') AS Barcode,
Item.ITEM_Desc AS ItemName,
isnull(item_Imagepath, '') AS ItemImage,
0 AS availableQty,
Item.SalesPrice AS Price,
isnull(Item.TaxApply, 0) AS isTax,
Item.TaxType,
ISNULL(Item.ITM_Type, '0') AS ITMType,
Item.Profit_Type AS ProfitType,
Item.Profit_Amt AS ProfitAmt,
Item.CostPrice,
ISNULL(Item.ITEM_Remarks, '') AS Remark
FROM Item

If it takes 10 seconds to read 6000 (small) records then I can only assume/hope that your connection is introducing a lot of delays here!?!
Could you try the following and see how long this takes?
SET STATISTICS TIME ON
SELECT * INTO #test FROM [Item]
If this runs quickly then I'd go look for ways to speed up your connection between your computer and the Azure severs. (network packet size?)

Related

Delete Statement Doesn't Use Index

I've got a table with the following schema...
CREATE TABLE [dbo].[Object1](
[id] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[Column1] [datetime] NOT NULL,
[Column2] [int] NOT NULL,
[Column4] [char](4) NOT NULL,
[Column5] [money] NOT NULL,
[Column6] [bigint] NOT NULL,
[Column7] [char](6) NOT NULL,
[Column3] [bit] NOT NULL,
[Column8] [bigint] NULL,
CONSTRAINT [pk_Object1] PRIMARY KEY NONCLUSTERED
([id] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF
, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON))
GO
ALTER TABLE [dbo].[Object1] ADD CONSTRAINT [DF_Object1_Column1] DEFAULT (getutcdate()) FOR [Column1]
GO
ALTER TABLE [dbo].[Object1] ADD CONSTRAINT [DF_Object1_Column3] DEFAULT ((0)) FOR [Column3]
GO
Each night, a delete query runs against this table...
delete from Object1
where ( Column1 < DATEADD(dd, -40, GETUTCDATE()) )
OR ( Column3 = ? and Column1 < DATEADD(dd, -3, GETUTCDATE()))
The table has
Index2 (id) - clustered, unique located on PRIMARY
Index3 (id) - nonclustered located on PRIMARY
Index1 (id) - nonclustered, unique, primary key located on
PRIMARY
Index4 (Column7) - nonclustered located on PRIMARY
Index5 (Column2, Column7, Column4, Column5) - nonclustered
located on PRIMARY
The query produces the following plan...
Execution Plan for Delete Statement
The statement causes blocking in a highly concurrent table. The blocking ripples into system errors.
If I recreate "Object1_IX" to cover the "Object1" field, this still happens. The table has 6,568,449-rows and trims about 200,000 a night.

Dead lock on table's primary key when running multiple of the same query

I have a query that is causing deadlocks on the primary key of a table. The query can run from multiple different code paths. New DB connections are created each time a query needs to be run, so this is not a connection reuse issue, as far as I can tell.
The query in question is
CREATE PROCEDURE [dbo].[upd_task] #taskid INTEGER
, #state INTEGER
, #args NVARCHAR( max )
, #retries INTEGER
, #started bit
, #finished bit
, #username NVARCHAR( 50 ) AS BEGIN BEGIN TRAN
UPDATE task WITH (updlock) SET
[state] = #state
, args = #args
, retries = #retries
, started = #started
, finished = #finished
, modifier = #username
, modified = GETDATE()
WHERE
task.task_id = #taskid COMMIT END
The table is defined as
CREATE TABLE [dbo].[task](
[task_id] [bigint] IDENTITY(1,1) NOT NULL,
[job_id] [bigint] NOT NULL,
[car_id] [int] NOT NULL,
[uuid] [nvarchar](60) NULL,
[user_priority] [bit] NOT NULL,
[args] [nvarchar](max) NULL,
[state] [int] NOT NULL,
[started] [bit] NOT NULL,
[finished] [bit] NOT NULL,
[failed] [bit] NOT NULL,
[error_msg] [nvarchar](max) NULL,
[retries] [int] NULL,
[exec_time] [datetime] NULL,
[created] [datetime] NULL,
[creator] [nvarchar](50) NULL,
[modified] [datetime] NULL,
[modifier] [nvarchar](50) NULL,
CONSTRAINT [PK_task_id] PRIMARY KEY CLUSTERED
(
[task_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [user_priority]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [state]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [started]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [finished]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [failed]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [retries]
GO
ALTER TABLE [dbo].[task] WITH CHECK ADD CONSTRAINT [FK_task_car] FOREIGN KEY([car_id])
REFERENCES [dbo].[car] ([car_id])
GO
ALTER TABLE [dbo].[task] CHECK CONSTRAINT [FK_task_car]
GO
ALTER TABLE [dbo].[task] WITH CHECK ADD CONSTRAINT [FK_task_job] FOREIGN KEY([job_id])
REFERENCES [dbo].[job] ([job_id])
GO
ALTER TABLE [dbo].[task] CHECK CONSTRAINT [FK_task_job]
GO
The dead lock graph, shown below, has two instances of the same query running [upd_task.e

Error while Switching Data from Master to Archive - Partition Function

I have two tables as follows where in the later is a table for storing the archived data.
CREATE TABLE [ERP].[Transaction](
[TransactionID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[TransactionDateTime] [datetime] NOT NULL,
[TransactionName] [int] NOT NULL,
[TransactionData] [xml] NOT NULL,
[AcknowledgementDateTime] [datetime] NULL,
[AcknowledgementCode] [int] NULL,
[AcknowledgementDescription] [nvarchar](max) NULL,
[TransactionGUID] [uniqueidentifier] NOT NULL,
[WorkCenter] [nvarchar](10) NULL,
CONSTRAINT [pkTransaction] PRIMARY KEY NONCLUSTERED
(
[TransactionID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
GO
ALTER TABLE [ERP].[Transaction] ADD CONSTRAINT [defTransactionTransactionGUID] DEFAULT (newid()) FOR [TransactionGUID]
GO
CREATE TABLE [ERP].[TransactionArchived](
[TransactionID] [int] IDENTITY(1,1) NOT NULL,
[TransactionDateTime] [datetime] NOT NULL,
[TransactionName] [int] NOT NULL,
[TransactionData] [xml] NOT NULL,
[AcknowledgementDateTime] [datetime] NULL,
[AcknowledgementCode] [int] NULL,
[AcknowledgementDescription] [nvarchar](max) NULL,
[TransactionGUID] [uniqueidentifier] NOT NULL,
[WorkCenter] [nvarchar](10) NULL
)
GO
I have made partition as follows and create Indexes to support the partition.
CREATE PARTITION FUNCTION PartitionXFaceDataByMonthPF ( DATETIME )
AS RANGE RIGHT FOR VALUES ( '2010-01-01', '2010-02-01', '2010-03-01', '2010-04-01', '2010-05-01', '2010-06-01', '2010-07-01', '2010-08-01', '2010-09-01', '2010-10-01', '2010-11-01', '2010-12-01',
'2011-01-01', '2011-02-01', '2011-03-01', '2011-04-01', '2011-05-01', '2011-06-01', '2011-07-01', '2011-08-01', '2011-09-01', '2011-10-01', '2011-11-01', '2011-12-01',
'2012-01-01', '2012-02-01', '2012-03-01', '2012-04-01', '2012-05-01', '2012-06-01', '2012-07-01', '2012-08-01', '2012-09-01', '2012-10-01', '2012-11-01', '2012-12-01',
'2013-01-01', '2013-02-01', '2013-03-01', '2013-04-01', '2013-05-01', '2013-06-01', '2013-07-01', '2013-08-01', '2013-09-01', '2013-10-01', '2013-11-01', '2013-12-01',
'2014-01-01', '2014-02-01', '2014-03-01', '2014-04-01', '2014-05-01', '2014-06-01', '2014-07-01', '2014-08-01', '2014-09-01', '2014-10-01', '2014-11-01', '2014-12-01',
'2015-01-01', '2015-02-01', '2015-03-01', '2015-04-01', '2015-05-01', '2015-06-01', '2015-07-01', '2015-08-01', '2015-09-01', '2015-10-01', '2015-11-01', '2015-12-01' ) ;
--Create the PARTITION SCHEME
CREATE PARTITION SCHEME PartitionXFaceDataByMonthPS AS PARTITION PartitionXFaceDataByMonthPF ALL TO ( [PRIMARY] ) ;
Index Creation
ALTER TABLE [ERP].[Transaction] DROP CONSTRAINT [pkTransaction] ;
ALTER TABLE [ERP].[Transaction] ADD CONSTRAINT [pkTransaction] PRIMARY KEY NONCLUSTERED ( [TransactionID] ASC ) ON [PRIMARY] ;
/****** Object: Index [idxTransactionDateTime] Script Date: 01/14/2016 15:53:28 ******/
IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[ERP].[Transaction]') AND name = N'idxTransactionDateTime')
DROP INDEX [idxTransactionDateTime] ON [ERP].[Transaction] WITH ( ONLINE = OFF )
GO
CREATE CLUSTERED INDEX idxTransactionDateTime ON [ERP].[Transaction]( [TransactionDateTime] ) ON PartitionXFaceDataByMonthPS( [TransactionDateTime] ) ;
--DROP TABLE [ERP].[TransactionArchived] ; --This needs to execute only if we rerun the same set onace again on the same DB
SELECT * INTO [ERP].[TransactionArchived] FROM [ERP].[Transaction] WHERE 1 = 2 ;
CREATE CLUSTERED INDEX idxTransactionDateTime ON [ERP].[TransactionArchived] ( TransactionDateTime ) ON PartitionXFaceDataByMonthPS( [TransactionDateTime] ) ;
--CREATE NONCLUSTERED INDEX [pkTransaction] ON [ERP].[TransactionArchived] ( [TransactionID] ) ON PartitionXFaceDataByMonthPS( [TransactionDateTime] ) ;
GO
When I am switching data it gives an error as follows which I am struggling to find a solution. Please help
'ALTER TABLE SWITCH' statement failed. The table 'EPMSDataExchange.ERP.Transaction' is partitioned while index 'pkTransaction' is not partitioned.
thanks
pep

deleting an employee conflicts with department manager foreign key constraint

I created two tables , departments and employees with the following structure :
CREATE TABLE [dbo].[departments](
[department_id] [bigint] IDENTITY(10,10) NOT NULL,
[department_name] [nvarchar](30) NOT NULL,
[manager_id] [bigint] NULL,
[location_id] [bigint] NULL,
[department_notes] [varchar](150) NULL,
[created_by] [bigint] NULL,
[created_date] [datetime] NULL,
[last_updated_by] [bigint] NULL,
[last_updated_date1] [datetime] NULL,
[status] [varchar](12) NOT NULL,
CONSTRAINT [PK_departments] PRIMARY KEY CLUSTERED
(
[department_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [UQ_NoRepeat] UNIQUE NONCLUSTERED
(
[department_name] ASC,
[location_id] ASC,
[status] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[departments] ADD CONSTRAINT [DF_departments_created_date] DEFAULT (getdate()) FOR [created_date]
GO
ALTER TABLE [dbo].[departments] ADD CONSTRAINT [DF_departments_created_date1] DEFAULT (getdate()) FOR [last_updated_date1]
GO
ALTER TABLE [dbo].[departments] ADD CONSTRAINT [DF_departments_status] DEFAULT ('Active') FOR [status]
GO
ALTER TABLE [dbo].[departments] WITH CHECK ADD FOREIGN KEY([manager_id])
REFERENCES [dbo].[employees] ([employee_id])
GO
ALTER TABLE [dbo].[departments] WITH CHECK ADD CONSTRAINT [FK_departments_locations] FOREIGN KEY([location_id])
REFERENCES [dbo].[locations] ([location_id])
GO
ALTER TABLE [dbo].[departments] CHECK CONSTRAINT [FK_departments_locations]
GO
ALTER TABLE [dbo].[departments] WITH CHECK ADD CONSTRAINT [Ck_deptStatus] CHECK (([Status]='Deleted' OR [Status]='Active'))
GO
ALTER TABLE [dbo].[departments] CHECK CONSTRAINT [Ck_deptStatus]
GO
CREATE TABLE [dbo].[employees](
[employee_id] [bigint] NOT NULL,
[first_name] [nvarchar](20) NULL,
[last_name] [nvarchar](25) NOT NULL,
[email] [nvarchar](25) NOT NULL,
[phone_number] [nvarchar](20) NULL,
[hire_date] [date] NULL,
[job_id] [nvarchar](10) NOT NULL,
[salary] [numeric](8, 2) NOT NULL,
[commission_pct] [numeric](2, 2) NULL,
[manager_id] [bigint] NULL,
[department_id] [bigint] NOT NULL,
[allow_login] [bit] NOT NULL,
[user_id] [nvarchar](128) NULL,
[allow_email] [bit] NOT NULL,
[driv_lic_no] [nchar](20) NULL,
[reporting_to] [bigint] NULL,
[salutation] [nchar](10) NULL,
[date_of_birth] [date] NULL,
[gender] [nchar](10) NULL,
[blood_group] [nchar](10) NULL,
[Nationality] [nchar](10) NULL,
[gov_id] [nchar](25) NULL,
[passport_no] [nchar](25) NULL,
[passport_expir] [date] NULL,
[driv_lic_expir] [date] NULL,
[perm_address] [varchar](250) NULL,
[perm_city] [varchar](50) NULL,
[perm_state] [varchar](50) NULL,
[per_zip] [nchar](20) NULL,
[perm_country] [nvarchar](6) NULL,
[current_address] [varchar](250) NULL,
[current_city] [varchar](50) NULL,
[current_state] [varchar](50) NULL,
[current_zip] [nchar](20) NULL,
[current_country] [nvarchar](6) NULL,
[mobile_no] [nvarchar](20) NULL,
[notes] [varchar](250) NULL,
[added_by] [bigint] NULL,
[added_on] [date] NULL,
[send_cred_by_email] [bit] NOT NULL,
[user_name] [nvarchar](256) NULL,
CONSTRAINT [PK_employees] PRIMARY KEY CLUSTERED
(
[employee_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[employees] ADD CONSTRAINT [DF_employees_allow_login] DEFAULT ((0)) FOR [allow_login]
GO
ALTER TABLE [dbo].[employees] ADD CONSTRAINT [DF_employees_allow_email] DEFAULT ((0)) FOR [allow_email]
GO
ALTER TABLE [dbo].[employees] ADD DEFAULT ('Mr.') FOR [salutation]
GO
ALTER TABLE [dbo].[employees] ADD DEFAULT ((1)) FOR [send_cred_by_email]
GO
ALTER TABLE [dbo].[employees] WITH CHECK ADD CONSTRAINT [FK_employees_AspNetUsers] FOREIGN KEY([user_id])
REFERENCES [dbo].[AspNetUsers] ([Id])
GO
ALTER TABLE [dbo].[employees] CHECK CONSTRAINT [FK_employees_AspNetUsers]
GO
ALTER TABLE [dbo].[employees] WITH CHECK ADD CONSTRAINT [FK_employees_countries] FOREIGN KEY([perm_country])
REFERENCES [dbo].[countries] ([country_id])
GO
ALTER TABLE [dbo].[employees] CHECK CONSTRAINT [FK_employees_countries]
GO
ALTER TABLE [dbo].[employees] WITH CHECK ADD CONSTRAINT [FK_employees_countries1] FOREIGN KEY([current_country])
REFERENCES [dbo].[countries] ([country_id])
GO
ALTER TABLE [dbo].[employees] CHECK CONSTRAINT [FK_employees_countries1]
GO
ALTER TABLE [dbo].[employees] WITH CHECK ADD CONSTRAINT [FK_employees_employees] FOREIGN KEY([employee_id])
REFERENCES [dbo].[employees] ([employee_id])
GO
ALTER TABLE [dbo].[employees] CHECK CONSTRAINT [FK_employees_employees]
GO
there were some existing employees which are already managers in departments table , now when I try to delete any of those manager employees, I get a foreign key constraint conflict error blocking the delete statement.
what is the problem please ?
The problem is that you're not allowed to do that.
A foreign key constraint is an integrity constraint meaning that it ensures data integrity across tables.
If one department row says that the ID of the employee that is the manager of this department is 73, it would not be good if there is no employee in the database with an ID of 73.
A foreign key constraint ensures this won't happen:
You cannot insert or update a department row to have a manager id that does not exist in the employee table
You cannot delete an employee that is referenced as a manager from a department row
You cannot update the ID of an employee that is referenced as a manager from a department row
The solution is to first fix the department by:
Switching to a different manager by updating the department row and adjusting the ID of the manager
NULL'ing out the manager, saying that this department has no manager, if that is allowed
After doing any of those two actions you should be able to delete the employee, assuming there's no other foreign key references as well.

Delete Query: 14 minute to no response

I have a Sql Azure database, everything was good from last 6 months until today, when a simple
Delete from ListData where ListID=2323
fail to delete 7500 records out of 1.9 Million records after 14 minutes of running query. However Select query take less than 2-3 second to list them all.
Previously the delete works much like select and it usually take less than 20 second to finish delete operation. something is wrong today only.
Complete database size is 1.1GB where as we have Web edition set at 5GB so, we have plenty of space available.
Any idea what is going wrong? that delete has cause some serious problem in system which cause my client lose quite a money.
Thanks for any guide.
Edit: I do have couple of Index on table, but no trigger, FK or any other such thing in table. LISTID is foreign key [logically], and RecordID [another column in table] is auto increment id in Listdata table.
*Edit 2 *:
/****** Object: Table [dbo].[tblSalesListData] Script Date: 02-07-2013 11:45:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[ListData](
[RecordID] [bigint] IDENTITY(1,1) NOT NULL,
[ListID] [bigint] NULL,
[SalesID] [bigint] NULL,
[UserID] [varchar](100) NULL,
[FirstName] [varchar](100) NULL,
[MiddleName] [varchar](50) NULL,
[LastName] [varchar](50) NULL,
[Address1] [varchar](100) NULL,
[Address2] [varchar](100) NULL,
[City] [varchar](100) NULL,
[State] [varchar](100) NULL,
[ZipCode] [varchar](10) NULL,
[WorkPhone] [varchar](15) NULL,
[HomePhone] [varchar](15) NULL,
[CellPhone] [varchar](15) NULL,
[Email] [varchar](100) NULL,
[DealerCode] [varchar](20) NULL,
[IsPrinted] [varchar](10) NULL,
[tag] [varchar](100) NULL,
[RecordDate] [datetime] NULL,
[CustomInfo] [text] NULL,
[SourceData] [text] NULL,
CONSTRAINT [PK_ListData] PRIMARY KEY CLUSTERED
(
[RecordID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[ListData] ADD DEFAULT ('N') FOR [IsPrinted]
GO
Try to add index and change table structure -
CREATE TABLE dbo.tblSalesListData
(
RecordID BIGINT IDENTITY(1,1) NOT NULL PRIMARY KEY,
ListID BIGINT NOT NULL, -- NULL --> NOT NULL
SalesID BIGINT NULL,
UserID VARCHAR(100) NULL,
FirstName VARCHAR(100) NULL,
MiddleName VARCHAR(50) NULL,
LastName VARCHAR(50) NULL,
Address1 VARCHAR(100) NULL,
Address2 VARCHAR(100) NULL,
City VARCHAR(100) NULL,
[State] VARCHAR(100) NULL,
ZipCode VARCHAR(10) NULL,
WorkPhone VARCHAR(15) NULL,
HomePhone VARCHAR(15) NULL,
CellPhone VARCHAR(15) NULL,
Email VARCHAR(100) NULL,
DealerCode VARCHAR(20) NULL,
IsPrinted VARCHAR(10) NULL,
tag VARCHAR(100) NULL,
RecordDate DATETIME NULL,
CustomInfo VARCHAR(MAX) NULL, -- TEXT --> VARCHAR(MAX)
SourceData VARCHAR(MAX) NULL -- TEXT --> VARCHAR(MAX)
)
GO
CREATE /*UNIQUE*/ NONCLUSTERED INDEX IX_ListID ON dbo.tblSalesListData
(
ListID ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
I have a little bit of same problem. I did the following step. but be sure to back up the database before doing these steps.
Create another table with identical structure
Insert the data in new table of the old table
Drop the old table
and try again to see how much time will it take.
Thanks for #Dinup and #Devart that give me idea, though I didn't fully follow their word, but both of them guide me in direction where I found solution as :
Delete all Indexes based on my ListID.
Run my Query, it take less than 1 second now.
Recreate my indexes.
Happy Living.

Resources