首页 > 编程技术 > php

SQL中in或or与union all性能对比

发布时间:2016-11-25 15:49

下面我人利用union all来替换in或or,有需要的朋友可参考一下。

使用or:

 代码如下 复制代码
WHERE * FROM article
WHERE article_category=2
OR article_category=3
ORDER BY article_id DESC
LIMIT 5
// 执行时间:11.0777

 
使用in:

 代码如下 复制代码

SELECT * FROM article
WHERE article_category IN (2,3)
ORDER BY article_id DESC
LIMIT 5

// 执行时间:11.2850
使用union all:

 代码如下 复制代码

(
    SELECT * FROM article
    WHERE article_category=2
    ORDER BY article_id DESC
    LIMIT 5
) UNION ALL (
    SELECT * FROM article
    WHERE article_category=3
    ORDER BY article_id DESC
    LIMIT 5
)
ORDER BY article_id DESC
LIMIT 5
// 执行时间:0.0261

本文章总结了几种利用php 清除换行符,清除制表符,去掉注释标记实现代码,有需要的朋友可参考本文章。
 代码如下 复制代码

<?php
 /**
  * 压缩html : 清除换行符,清除制表符,去掉注释标记  
  * @param   $string  
  * @return  压缩后的$string 
  * */
 function compress_html($string) {  
     $string = str_replace("rn", '', $string); //清除换行符  
     $string = str_replace("n", '', $string); //清除换行符  
     $string = str_replace("t", '', $string); //清除制表符  
     $pattern = array (  
                     "/> *([^ ]*) *</", //去掉注释标记  
                     "/[s]+/",  
                     "/<!--[^!]*-->/",  
                     "/" /",  
                     "/ "/",  
                     "'/*[^*]**/'" 
                     );  
     $replace = array (  
                     ">\1<",  
                     " ",  
                     "",  
                     """,  
                     """,  
                     "" 
                     );  
     return preg_replace($pattern, $replace, $string);  
 }

去除连续的空格和换行符

 代码如下 复制代码

<?php
$str="i   am    a     booknnnnnmoth";
//去除所有的空格和换行符
echo preg_replace("/[s]{2,}/","",$str).'<br>';
//去除多余的空格和换行符,只保留一个
echo preg_replace("/([s]{2,})/","\1",$str);
?>

去除回车换行符

preg_replace("'([rn])[s]+'", "", $content) //去除回车换行符

 代码如下 复制代码

<?php
// $document 应包含一个 HTML 文档。
// 本例将去掉 HTML 标记,javascript 代码
// 和空白字符。还会将一些通用的
// HTML 实体转换成相应的文本。

$search = array ("'<script[^>]*?>.*?</script>'si",  // 去掉 javascript
                 "'<[/!]*?[^<>]*?>'si",           // 去掉 HTML 标记
                 "'([rn])[s]+'",                 // 去掉空白字符
                 "'&(quot|#34);'i",                 // 替换 HTML 实体
                 "'&(amp|#38);'i",
                 "'&(lt|#60);'i",
                 "'&(gt|#62);'i",
                 "'&(nbsp|#160);'i",
                 "'&(iexcl|#161);'i",
                 "'&(cent|#162);'i",
                 "'&(pound|#163);'i",
                 "'&(copy|#169);'i",
                 "'&#(d+);'e");                    // 作为 PHP 代码运行

$replace = array ("",
                  "",
                  "\1",
                  """,
                  "&",
                  "<",
                  ">",
                  " ",
                  chr(161),
                  chr(162),
                  chr(163),
                  chr(169),
                  "chr(\1)");

$text = preg_replace ($search, $replace, $document);
?>

下面我们来介绍一下关于php实现断点续传的代码,有需要学习的朋友可参考一下。

让PHP下载代码支持断点续传 主要靠的 HTTP协议中header Content-Range来实现
先来说说 HTTP的下载原理
对于HTTP协议,向服务器请求某个文件时,只要发送类似如下的请求即可:

 代码如下 复制代码

GET /Path/FileName HTTP/1.0
Host: www.server.com:80

Accept: **表示接收任何类型的数据。User-Agent表示用户代理,这个字段可有可无,但强烈建议加上,因为它是服务器统计、追踪以及识别客户端的依据。Connection字段中的close表示使用非持久连接。
关于HTTP协议更多的细节可以参考RFC2616(HTTP 1.1)。因为我只是想通过HTTP协议实现文件下载,所以也只看了一部分,并没有看全。
如果服务器成功收到该请求,并且没有出现任何错误,则会返回类似下面的数据:

 代码如下 复制代码
HTTP/1.0 200 OK
Content-Length: 13057672
Content-Type: application/octet-stream
Last-Modified: Wed, 10 Oct 2005 00:56:34 GMT
Accept-Ranges: bytes
ETag: "2f38a6cac7cec51:160c"
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Wed, 16 Nov 2005 01:57:54 GMT
Connection: close


先定义一个函数 getRange() 这个函数用来处理 header中 Range 具体数据的处理

 代码如下 复制代码
/** $file_size  文件大小 */
 function getRange($file_size){
    $range = isset($_SERVER['HTTP_RANGE'])?$_SERVER['HTTP_RANGE']:null;
    if(!empty($range)){
        $range = preg_replace('/[s|,].*/', '', $range);
        $range = explode('-',substr($range,6));
        if (count($range) < 2 ) {
            $range[1] = $file_size;
        }
        $range = array_combine(array('start','end'),$range);
        if (empty($range['start'])) {
            $range['start'] = 0;
        }
        if (!isset ($range['end']) || empty($range['end'])) {
            $range['end'] = $file_size;
        }
        return $range;
    }
    return null;
}

假设文件的地址为 $file_path

 代码如下 复制代码

$speed = 512;//此参数为下载最大速度
$pos = strrpos($file_path, "/");
$file_name = substr($file_path, $pos+1);
$file_size = filesize($file_path);
$ranges = getRange($file_size);
$fh =  fopen($file_path, "rb");
header('Cache-control: public');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$file_name);
if ($ranges != null) {
    header('HTTP/1.1 206 Partial Content');
    header('Accept-Ranges: bytes');
    header(sprintf('Content-Length: %u',$ranges['end'] - $ranges['start']));
    header(sprintf('Content-Range: bytes %s-%s/%s', $ranges['start'], $ranges['end'], $file_size));
    fseek($fh, sprintf('%u',$ranges['start']));
}else{
    header("HTTP/1.1 200 OK");
    header(sprintf('Content-Length: %s', $file_size));
}
while(!feof($fh))
{
    echo  fread($fh, round($speed*1024, 0));
    ob_flush();
    sleep(1);
}
($fh != null) && fclose($fh);

基本如此 就可以解决一般性文件的断点续传或者下载

一个可以统计你程序的运行时间长知的php类,有需要的朋友可参考一下。
 代码如下 复制代码
class Timer { 
    private $StartTime = 0;//程序运行开始时间
    private $StopTime  = 0;//程序运行结束时间
    private $TimeSpent = 0;//程序运行花费时间
    function start(){//程序运行开始
        $this->StartTime = microtime(); 
    } 
    function stop(){//程序运行结束
        $this->StopTime = microtime(); 
    } 
    function spent(){//程序运行花费的时间
        if ($this->TimeSpent) { 
            return $this->TimeSpent; 
        } else {
         list($StartMicro, $StartSecond) = explode(" ", $this->StartTime);
         list($StopMicro, $StopSecond) = explode(" ", $this->StopTime);
            $start = doubleval($StartMicro) + $StartSecond;
            $stop = doubleval($StopMicro) + $StopSecond;
            $this->TimeSpent = $stop - $start;
            return substr($this->TimeSpent,0,8)."秒";//返回获取到的程序运行时间差
        } 
    } 

$timer = new Timer(); 
$timer->start();
//...程序运行的代码
$timer->stop();
echo "程序运行时间为:".$timer->spent();
php中global和static两个关键字详解,有需要的朋友可参考一下。

1.global在整个页面起作用。
2.static只在function和class内起作用。
global和$GLOBALS使用基本相同,但在实际开发中大不相同。
global在函数产生一个指向函数外部变量的别名变量,而不是真正的函数外部变量,一但改变了别名变量的指向地址,就会发生一些意料不到情况,例如例子1.
$GLOBALS[]确确实实调用是外部的变量,函数内外会始终保持一致!

实例

 代码如下 复制代码

<?php
// 比较global、GLOBALS、static
$k = 0;
function test1() {
 global $k;
 static $i = 0;
 echo 'i:', ++$i, '<br/>';
 echo 'k:', ++$k, '<br/>';
}
test1();
test1();
echo 'i:', $i, '<br/>';
echo 'k:', $k, '<br/>';

echo '------------------<br/>';

function test2() {
 global $k;
 static $i = 0;
 echo 'i:', ++$i, '<br/>';
 echo 'k:', ++$k, '<br/>';
}
test2();
test2();
echo 'i:', $i, '<br/>';
echo 'k:', $k, '<br/>';

$m = 0;
$n = 0;
function test3() {
 global $m;
 echo 'm:', $m++ , '<br/>';
 echo 'n:', $GLOBALS['n']++ , '<br/>';
 unset($m, $GLOBALS['n']);
}
echo '------------------<br/>';
test3();

echo 'm:', $m, '<br/>';
echo 'n:', $n, '<br/>';

echo '------------------<br/>';

static $x = 0;
function test4() {
 echo 'x:', $x++ , '<br/>';
}

test4();

结果如下:

i:1
k:1
i:2
k:2
i:
k:2
------------------
i:1
k:3
i:2
k:4
i:
k:4
------------------
m:0
n:0
m:1
n:
------------------
x:

global关键字如果用在function内部,则说明这个function内用的这个变量是全局的,全局变量就是在整个页面里都能起作用
static就是表示静态。所谓的静态,其实是在function或者class内部而言的。function中static的变量,在funciton执行完之后,不会消失,可以在下次执行的时候继续使用

标签:[!--infotagslink--]

您可能感兴趣的文章: