Link to home
Start Free TrialLog in
Avatar of wattkey
wattkey

asked on

odd SQL query performance is causing issues

I have one particular query that takes over 1 minute to run. But other queries almost the exact same run in less than a 1 second. Its really confusing me and not sure how to speed this up.

This is running on MS SQL 2000.

I've tried re-indexing and reseting statistics.

 
-- This query takes over a mintue to run 
SELECT 
tblInboundPaper.BookingID, tblInboundPaper.BookingNum, tblProducts.ProductDesc, tblInboundPaper.CustomerID
FROM tblInboundPaper 
inner join tblProducts
ON tblProducts.ProductID = tblInboundPaper.ProductID 
INNER JOIN tblInboundPaperRolls 
ON tblInboundPaper.BookingID = tblInboundPaperRolls.BookingID  
INNER JOIN tblOutboundPaperRolls 
ON tblInboundPaperRolls.InboundBOLID = tblOutboundPaperRolls.InboundBOLID 
WHERE tblInboundPaper.CustomerID = 336
GROUP BY tblInboundPaper.BookingID, tblInboundPaper.BookingNum, tblProducts.ProductDesc, tblInboundPaper.CustomerID
ORDER BY tblInboundPaper.BookingNum  

-- This query takes less than a second 
-- This customer has many more records associated with it
SELECT 
tblInboundPaper.BookingID, tblInboundPaper.BookingNum, tblProducts.ProductDesc, tblInboundPaper.CustomerID
FROM tblInboundPaper 
inner join tblProducts
ON tblProducts.ProductID = tblInboundPaper.ProductID 
INNER JOIN tblInboundPaperRolls 
ON tblInboundPaper.BookingID = tblInboundPaperRolls.BookingID  
INNER JOIN tblOutboundPaperRolls 
ON tblInboundPaperRolls.InboundBOLID = tblOutboundPaperRolls.InboundBOLID 
WHERE tblInboundPaper.CustomerID = 132
GROUP BY tblInboundPaper.BookingID, tblInboundPaper.BookingNum, tblProducts.ProductDesc, tblInboundPaper.CustomerID
ORDER BY tblInboundPaper.BookingNum  

-- Comment out the customer criteria and it runs in less -- than a second

SELECT 
tblInboundPaper.BookingID, tblInboundPaper.BookingNum, tblProducts.ProductDesc, tblInboundPaper.CustomerID
FROM tblInboundPaper 
inner join tblProducts
ON tblProducts.ProductID = tblInboundPaper.ProductID 
INNER JOIN tblInboundPaperRolls 
ON tblInboundPaper.BookingID = tblInboundPaperRolls.BookingID  
INNER JOIN tblOutboundPaperRolls 
ON tblInboundPaperRolls.InboundBOLID = tblOutboundPaperRolls.InboundBOLID 
--WHERE tblInboundPaper.CustomerID = 336
GROUP BY tblInboundPaper.BookingID, tblInboundPaper.BookingNum, tblProducts.ProductDesc, tblInboundPaper.CustomerID
ORDER BY tblInboundPaper.BookingNum  

-- comment out the group by it runs in less than a second
SELECT 
tblInboundPaper.BookingID, tblInboundPaper.BookingNum, tblProducts.ProductDesc, tblInboundPaper.CustomerID
FROM tblInboundPaper 
inner join tblProducts
ON tblProducts.ProductID = tblInboundPaper.ProductID 
INNER JOIN tblInboundPaperRolls 
ON tblInboundPaper.BookingID = tblInboundPaperRolls.BookingID  
INNER JOIN tblOutboundPaperRolls 
ON tblInboundPaperRolls.InboundBOLID = tblOutboundPaperRolls.InboundBOLID 
WHERE tblInboundPaper.CustomerID = 336
--GROUP BY tblInboundPaper.BookingID, --tblInboundPaper.BookingNum, tblProducts.ProductDesc, --tblInboundPaper.CustomerID
ORDER BY tblInboundPaper.BookingNum  


-- Wow this even works in 1 second until you un-comment out the where clause then 1 minutes plus
select 
* 
from
(
	SELECT 
	tblInboundPaper.BookingID, tblInboundPaper.BookingNum, tblProducts.ProductDesc, tblInboundPaper.CustomerID
	FROM tblInboundPaper 
	inner join tblProducts
	ON tblProducts.ProductID = tblInboundPaper.ProductID 
	INNER JOIN tblInboundPaperRolls 
	ON tblInboundPaper.BookingID = tblInboundPaperRolls.BookingID  
	INNER JOIN tblOutboundPaperRolls 
	ON tblInboundPaperRolls.InboundBOLID = tblOutboundPaperRolls.InboundBOLID 
	GROUP BY tblInboundPaper.BookingID, tblInboundPaper.BookingNum, tblProducts.ProductDesc, tblInboundPaper.CustomerID
	 
) as t
--where CustomerID = 336
ORDER BY BookingNum

Open in new window

Avatar of JuanCarniglia
JuanCarniglia
Flag of Argentina image

There has to be and issue with tblInboundPaper. Check that CustomerID is an INT and that the main index is set up on that field.

Also, why do you GROUP By all fields, if you don't SUM, COUNT or anything? You may want to try using DISTINCT instead.

Greetings.
Avatar of Imran Javed Zia
Please try this,
It seems  tblProducts.ProductDesc, has very large data.

SELECT   distinct
tblInboundPaper.BookingID, tblInboundPaper.BookingNum, tblProducts.ProductDesc, tblInboundPaper.CustomerID  
FROM tblInboundPaper  
inner join tblProducts  
ON tblProducts.ProductID = tblInboundPaper.ProductID  
INNER JOIN tblInboundPaperRolls  
ON tblInboundPaper.BookingID = tblInboundPaperRolls.BookingID    
INNER JOIN tblOutboundPaperRolls  
ON tblInboundPaperRolls.InboundBOLID = tblOutboundPaperRolls.InboundBOLID  
WHERE tblInboundPaper.CustomerID = 336  
ORDER BY tblInboundPaper.BookingNum
Avatar of wattkey
wattkey

ASKER

customerId is an INT

The main index (primary key) on tblInboundPaper is BookingId

distinct makes no performance difference over GROUP By

productDescr is a varchar(50) and there are 600 entries in the table, i don't think that is the problem
For a minute, let's forget about the comparisons, I do not believe they are germaine. Please post the schema for all the tables including the indexes on each one.
Also, check that there are no current LOCKS on the database (If possible, restart the engine).

I know it's kind of radical, and you are only selecting, but... who knows.
Avatar of wattkey

ASKER


The performance has degrade even more in the past few days. it now takes it over 3 minutes for that query to run, the others are still fine. I am going to restart the SQL server overnight and see if that makes a difference. If not I will post the schemas acperkins has asked for.
>>I am going to restart the SQL server overnight and see if that makes a difference.<<
It will.  For a day or two and then you will be back to the same problem.
Avatar of wattkey

ASKER

Here are the create statements to make the said tables

 
CREATE TABLE [dbo].[tblInboundPaper](
	[BookingID] [int] IDENTITY(1,1) NOT NULL,
	[BookingNum] [nvarchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[CustomerID] [int] NULL,
	[ProductID] [int] NULL,
PRIMARY KEY CLUSTERED 
(
	[BookingID] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]



GO
CREATE TABLE [dbo].[tblInboundPaperRolls](
	[InboundBOLID] [int] IDENTITY(1,1) NOT NULL,
	[BookingID] [int] NULL,
	[DateReceived] [smalldatetime] NULL,
	[InboundBOLNum] [nvarchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[OrderNum] [nvarchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[RollPrefix] [nvarchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[RollNum] [nvarchar](15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[InboundLoad] [float] NULL,
	[BillableLoad] [float] NULL,
	[OutboundLoad] [float] NULL,
	[Width] [nvarchar](15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[Diameter] [nvarchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[BasisWeight] [nvarchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[InventoryFreeDays] [smallint] NULL,
PRIMARY KEY CLUSTERED 
(
	[InboundBOLID] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]


GO
CREATE TABLE [dbo].[tblProducts](
	[ProductID] [int] IDENTITY(1,1) NOT NULL,
	[ProductTypeID] [int] NULL,
	[ProductDesc] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
PRIMARY KEY CLUSTERED 
(
	[ProductID] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]


GO
CREATE TABLE [dbo].[tblOutboundPaperRolls](
	[OutboundBOLItemID] [int] IDENTITY(1,1) NOT NULL,
	[OutboundBOLID] [int] NULL,
	[InboundBOLID] [int] NULL,
PRIMARY KEY CLUSTERED 
(
	[OutboundBOLItemID] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]

Open in new window

Any additional indexes on these tables?
ASKER CERTIFIED SOLUTION
Avatar of JuanCarniglia
JuanCarniglia
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wattkey

ASKER

I am going to add the extra indexes, these should be non-clustered indexes?
>>these should be non-clustered indexes? <<
They have to be non-clustered.  You already have a clustered index on each table.