PHP MSSQL Paging SQL statement Optimization continuous Impact _php Tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
/**
* @Filename:p age.sql.class.php
* @CreatTime: 2009-01-06
* @Descrition: This class handles classes for SQL statements.
* @UpdateTime-1: null
* @Version: jswweb1.0.0
* @Author: Fkedwgwy
* @Dome:
$sql//sql Statements
$allcount//Total Record Count
$pagesize//page shows the number of record bars
$page//Current page
$SQLC = new Sqlpage ($sql, $allcount, $pagesize, $page);
$sql = $SQLC->getsql ();
The optimized statement:
SELECT * FROM (select top of LSH,ZTM,DYZRSM,DYZZFS,CBSM,CBNY,SSH,FBSL,JCSL from TS_GCB where Ssh Li Ke ' c% ' ORDER by Lsh ASC) as Inner_tbl ORDER by Lsh DESC) as Outer_tbl ORDER by Lsh ASC
*/
Class sqlpage{
function Sqlpage ($sql, $allcount, $pagesize, $page) {
$this->sql= $sql;//Query Language name
$this->allcount= intval ($allcount);//Total Record Count
$this->pagesize= intval ($pagesize);//page size (shows the number of records)
$this->page= intval ($page);//Current page
$this->getpage ();
$this->gettop ();
}
function GetPage () {//Get current page
$this->allpage=ceil ($this->allcount/$this->pagesize);//The largest integer to go to the current decimal
if ($this->page== "" or $this->page> $this->allpage or $this->page<0 or $this->page==0) {
$this->page2=1;
}else{
$this->page2=intval ($this->page);//Convert page numbers to numbers
}
}
function GetTop () {//Gets the top size of subquery 2
if ($this->page2< $this->allpage) {
$this->top2= $this->pagesize;
}else{
$this->top2= $this->allcount-$this->pagesize* ($this->allpage-1);
}
}
/* Function GetSQL () {//Get SQL statement
$this->s=preg_replace ("/select/i", "", $this->sql);
$this->top1= $this->pagesize* $this->page2;
$this->sql1= "Select TOP $this->top1 $this->s";
if (Strpos ($this->sql, "ASC")) {//Ascending
$this->sql_e= "SELECT * FROM (select TOP $this->top2 * FROM ($this->sql1) as Asystable ORDER by $this->order DESC) as Bsystable ORDER by $this->order ASC ";
}else
$this->sql_e= "SELECT * FROM (select TOP $this->top2 * FROM ($this->sql1) as Asystable ORDER by $this->ord Er DESC) as bsystable ORDER by $this->order ASC ";
if (Strpos ($this->sql, "desc")) {//Descending
$this->sql_e= "SELECT * FROM (select TOP $this->top2 * FROM ($this->sql1) as Asystable ORDER by $this->order ASC) as Bsystable ORDER by $this->order desc ";
}else{//does not handle sorting situations
$this->sql_e= "SELECT * FROM (select TOP $this->top2 * FROM ($this->sql1) as Asystable ORDER by $this->order DESC) as Bsystable ORDER by $this->order ASC ";
}
Echo $this->sql_e;
return $this->sql_e;
}*/
function GetSQL ()
{
$sql = $this->sql;
$this->top1= $this->pagesize* $this->page2;
$orderby = Stristr ($sql, ' ORDER by ');
if ($orderby!== false) {
$sort = (Stripos ($orderby, ' desc ')!== false)? ' desc ': ' ASC ';
$order = Str_ireplace (' ORDER by ', ', $orderby);
$order = Trim (preg_replace ('/\basc\b|\bdesc\b/i ', ' ', $order));
}
$sql = preg_replace ('/^select\s/i ', ' SELECT TOP '. ($this->top1). ", $sql);
$sql = ' SELECT ' From (select TOP '. $this->top2. ' * FROM ('. $sql. ') as Inner_tbl ';
if ($orderby!== false) {
$sql. = ' ORDER by '. $order. ' ';
$sql. = (Stripos ($sort, ' ASC ')!== false)? ' DESC ': ' ASC ';
}
$sql. = ') as Outer_tbl ';
if ($orderby!== false) {
$sql. = ' ORDER by '. $order. ' ' . $sort;
}
Echo $sql;
return $sql;
}
}
?>

http://www.bkjia.com/PHPjc/319975.html www.bkjia.com true http://www.bkjia.com/PHPjc/319975.html techarticle Copy the code as follows: PHP/** * @Filename:p age.sql.class.php * @CreatTime: 2009-01-06 * @Descrition: This class handles the class for the SQL statement. * @UpdateTime-1: null * @Version: Jswweb1 ...

  • 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.