I'm Currently using SQL Server 2008 R2 and ASP.Net with Linq
On Server 1
We have database with a table that contains address info for about a million records.
On Server 2
I have a customer table with about 1/2 million records which contains an AddressID
I have created a synonym to link to Server 1 for the address table. And have created a view to link both tables together within Server 2.
A stored procedure to handle paging of the data so that it returns me a page of 20 records, also tried adding RowNumber into the actual view which made no speed improvements
CREATE PROCEDURE [dbo].[GetCustomerAddressesByPage]
(
@pageIndex int,
@pageSize int,
@totalRows int output
)
AS
BEGIN
DECLARE @startRowIndex int
DECLARE @endRowIndex int
SET @startRowIndex = (@pageIndex * @pageSize) + 1;
SET @endRowIndex = (@pageIndex + 1) * @pageSize;
--SELECT * FROM
-- (SELECT ROW_NUMBER() OVER (ORDER BY CustomerID) AS RowNumber,*
-- FROM View_CustomersAddress) xx
--WHERE RowNumber >= @startRowIndex AND RowNumber <= @endRowIndex
SELECT *
FROM View_CustomersAddress
WHERE RowNum >= @startRowIndex AND RowNum <= @endRowIndex
SELECT @totalRows = COUNT(*) FROM View_CustomersAddress
END
This returns me the data within 2 seconds, but to handle the number of pages I also need the total rows and the following bit of code is taking another 20 or so seconds to complete
SELECT @totalRows = COUNT(CustomerID) FROM View_CustomersAddress
Server code is as follows to populate the data within a gridview
public IQueryable<GetCustomerAddressLinesByPageResult2> GetAddress(int startRowIndex, int maximumRows)
{
var data = dbContext.GetCustomerAddressLinesByPage(startRowIndex, maximumRows);
return data;
}
The view doesn't contain any indexing as you can't due to using a synonym, so I'm currently storing the total rows in a viewstate in my asp.net program so on initial load it takes 25 seconds+ but paging is fine.
Is there anyway to increase the performance of the count() or maybe another way of doing it without using count() which I've not thought of.
Just to clarify
Address table on Server 1 has a primary key AddressID and is unique
Customer table on Server 2 has a primary key CustomerID and is unique
The View_CustomersAddress does not have any index due to the synonym between Server 1 and Server 2