Paging of MySQL's efficient access process

Source: Internet
Author: User

Data over is 4-5 times faster than normal paging.

 

Procedure p_pagelist (m_pageno int, m_perpagecnt int, m_indexcolumn varchar (100), m_column varchar (1000), m_table varchar (1000), m_condition varchar (1000), m_orderby varchar (200 ), inout m_totalpagecnt INT)
Begin

If m_totalpagecnt> = 1 then
Set @ sqlcnt = Concat ('select count (1) into @ pagecnt from', m_table ); -- {*********************************************************************
If m_condition is not null and m_condition <> ''then
Set @ sqlcnt = Concat (@ sqlcnt, 'where', m_condition );
End if;
Prepare s_cnt from @ sqlcnt;
Execute s_cnt;
Deallocate prepare s_cnt;
Set m_totalpagecnt = @ pagecnt;
Else
Set @ pagecnt = 1; -- interval» è ° interval °
Set @ limitstart = (m_pageno-1) * m_perpagecnt;
Set @ limitend = m_perpagecnt;
Set @ SQL = Concat ('select', m_column, 'from', m_table); -- ää» ~~~~~~~
Set @ wsql = Concat ('select', m_indexcolumn, 'from', m_table); -- revoke *************
If m_condition is not null and m_condition <> ''then
Set @ SQL = Concat (@ SQL, 'where', m_condition );
Set @ wsql = Concat (@ wsql, 'where', m_condition );
End if;

If @ limitstart <1000 then
If m_orderby is not null and m_orderby <> ''then
Set @ SQL = Concat (@ SQL, 'ORDER BY', m_orderby );
End if;
Set @ SQL = Concat (@ SQL, 'limit', @ limitstart, ',', @ limitend );
Else
If m_orderby is not null and m_orderby <> ''then
Set @ wsql = Concat (@ wsql, 'ORDER BY', m_orderby );
If m_condition is not null and m_condition <> ''then
Set @ SQL = Concat (@ SQL, 'and', m_indexcolumn, '> = (', @ wsql, 'limit', @ limitstart, ', 1 )', 'ORDER BY', m_orderby, 'limit', @ limitend );
Else
Set @ SQL = Concat (@ SQL, 'where', m_indexcolumn, '> = (', @ wsql, 'limit', @ limitstart, ', 1 )', 'ORDER BY', m_orderby, 'limit', @ limitend );
End if;
Else
If m_condition is not null and m_condition <> ''then
Set @ SQL = Concat (@ SQL, 'and', m_indexcolumn, '> = (', @ wsql, 'limit', @ limitstart, ', 1 )', 'limit', @ limitend );
Else
Set @ SQL = Concat (@ SQL, 'where', m_indexcolumn, '> = (', @ wsql, 'limit', @ limitstart, ', 1 )', 'limit', @ limitend );
End if;
End if;
End if;

Prepare record from @ SQL;
Execute record;
Deallocate prepare record;

 

/*

Select @ SQL;
*/

 

 

 

End if;
End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.