Paging browsing is a common Web application feature that can be easily implemented with limit statements for MySQL databases, and for SQL Server databases, a common approach is to use a cursor in the dataset itself to implement paging, which is no problem for small amounts of data. But for a slightly larger amount of data, such as hundreds of thousands of data, the query speed will be reduced a lot, here I introduce a common technique, as long as a simple reconstruct query SQL statements, can greatly improve the query performance.
In the page-splitting algorithm, the key factor affecting query speed is to return the size of the dataset, we first set a primary key named ID in the datasheet, the value is an increment of integers, and then by refactoring the query SQL statements, we can implement the optimization of SQL query, the refactoring of SQL is as follows
The following are the referenced contents:
Select Top Page Size * from
table1
where id<=
(select min (id) from
((page 1) * page size) ID from table1 o Rder by id desc) as T
, ORDER by
ID DESC
The following JSP demo code, intpagesize for page size, intpage, id as the primary key, demonstrated the operation of a t_product table, and add various query conditions after the main statement of refactoring SQL, after the actual debugging, After this simple optimization, the SQL query speed is much higher than the query speed before the optimization.
The following are the referenced contents:
String sql= "from T_product where 1=1 and";
String ProductName = Request.getparameter ("ProductName");
if (productname!=null) sql=sql+ "ProductName like '%" + ProductName + "% ' and";
Sql=sql.substring (0,sql.length ()-4); Remove the trailing and string
sql= "select top" + string.valueof (intpagesize) + "*" +sql+ "and ID <= (select min (id) from (selec T Top "+ string.valueof (intpage*intpagesize) +" id "+ SQL +" ORDER BY id DESC "as T)"; Speed sql=sql+ through subqueries "ORDER by
ID DESC";