June 4, 2015 at 6:10 am
nil
June 4, 2015 at 6:17 am
I believe Chris asked for the execution plan already. Table definitions and index definitions are also kinda useful.
Why a left join? Do you really have invoices that don't have customers?
Why an unfiltered list of every single invoice in your system going right back to the beginning? Who's going to use such a list?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 4, 2015 at 6:26 am
mohanaprabhu.v (6/4/2015)
SELECT tblInvoice.*, tblCustomer.FirstName + SPACE(1) +tblCustomer.LastName as CustomerName FROM tblInvoice WITH(NOLOCK)
LEFT JOIN tblCustomer WITH(NOLOCK) ON tblInvoice.CustomerNo = tblCustomer.CustomerNo
See the link in my signature about the best way to get help. DDL and sample data would help.
In the meantime... if you can add a WHERE clause to filter out unneeded rows that might speed things up and maybe even get you an index seek. An index onblCustomer.CustomerNo that in includes blCustomer.FirstName and LastName might speed things up too.
If it's important that your query always return the correct data then those NOLOCK hints will have to be removed.
-- Itzik Ben-Gan 2001
June 4, 2015 at 6:43 am
nil
June 4, 2015 at 6:48 am
mohanaprabhu.v (6/4/2015)
SELECT tblInvoice.*, tblCustomer.FirstName + SPACE(1) +tblCustomer.LastName as CustomerName FROM tblInvoice
LEFT JOIN tblCustomer ON tblInvoice.CustomerNo = tblCustomer.CustomerNo WHERE
TransactionDate = '2014-10-23' AND ReceiptNo = '1962' AND TillNo = '004' AND StoreNo = '1101'
the query doesn't properly alias all the columns int he WHERE.
here's an assumption: all the WHERe are on tblInvoice:
SELECT tblInvoice.*,
tblCustomer.FirstName + Space(1)
+ tblCustomer.LastName AS customername
FROM tblInvoice
LEFT JOIN tblCustomer
ON tblInvoice.CustomerNo = tblCustomer.CustomerNo
WHERE tblInvoice.TransactionDate = '2014-10-23'
AND tblInvoice.ReceiptNo = '1962'
AND tblInvoice.TillNo = '004'
AND tblInvoice.StoreNo = '1101'
if that was true, an index like this might help, but it totally depends ont he selectivity of the columns:
CREATE INDEX IX_tblInvoice_FourColumns ON tblInvoice(TransactionDate,ReceiptNo,TillNo,StoreNo) INCLUDE(CustomerNo)
if you don't need ALL the columns from tblInvoice (if you don't need ALL the columns from tblInvoice.*, and can do just the specific columns you really need, you could make that same index supply the columns for you.
CREATE INDEX IX_tblInvoice_FourColumns ON tblInvoice(TransactionDate,ReceiptNo,TillNo,StoreNo) INCLUDE(CustomerNo,InvoiceNumber,OtherColumns)
Lowell
June 4, 2015 at 6:57 am
mohanaprabhu.v (6/4/2015)
SELECT tblInvoice.*, tblCustomer.FirstName + SPACE(1) +tblCustomer.LastName as CustomerName FROM tblInvoice
LEFT JOIN tblCustomer ON tblInvoice.CustomerNo = tblCustomer.CustomerNo WHERE
TransactionDate = '2014-10-23' AND ReceiptNo = '1962' AND TillNo = '004' AND StoreNo = '1101'
And the execution plan?
The table definitions?
The index definitions?
And is this the actual query this time?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 4, 2015 at 7:14 am
For a handful of reasons, besides performance, it's best not to return all columns from a table using * but rather to specify only the minimum columns your application or report requires. For example, if all your report needs is invoice number, order date, and customer name; that could be covered by an index on each table.
"Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply