mysql 문자열에 대해 like,INSTR, 범위, between 중에 제일 빠른것은? 정보
mysql 문자열에 대해 like,INSTR, 범위, between 중에 제일 빠른것은?첨부파일
본문
mysql 문자열에 대해 like,INSTR, 범위, between 값에 대한 소요 시간 검사
빠른순서 결론 between 방식이 제일 빠르고 INSTR 가 제일 느리다.
MySQL 버전 : mysqlnd 5.0.12
PHP 버전 : 7.2
where INSTR(wr_datetime, '2017') 대신
substring(wr_datetime,1,4)='2017' 을 사용해도 INSTR과 비슷한 시간이 걸렸고,
wr_datetime 대신 wr_subject, wr_content 를 검색해도 하니 INSTR가 제일 느리다 ㅠ
총 테이블수 79 / 총 레코드 :59,488 반복수 100
소요시간: 5.09초 방식: where wr_datetime like '%2017%'
소요시간: 6.28초 방식: where INSTR(wr_datetime, '2017')
소요시간: 3.99초 방식: where wr_datetime > '2017-00-00 00:00:00' and wr_datetime < '2018-00-00 00:00:00'
소요시간: 3.69초 방식: where wr_datetime between '2017-00-00 00:00:00' and '2018-00-00 00:00:00'
총 테이블수 79 / 총 레코드 :59,488 반복수 100
소요시간: 10.16초 방식: where wr_subject like '2017%'
소요시간: 14.54초 방식: where INSTR(wr_subject, '2017')
소요시간: 10.86초 방식: where substring(wr_subject,1,4)='2017'
소요시간: 10.84초 방식: where wr_subject > '2017-00-00 00:00:00' and wr_subject < '2018-00-00 00:00:00'
소요시간: 10.36초 방식: where wr_subject between '2017-00-00 00:00:00' and '2018-00-00 00:00:00'
총 테이블수 79 / 총 레코드 :59,488 반복수 100
소요시간: 10.04초 방식: where wr_content like '2017%'
소요시간: 50.99초 방식: where INSTR(wr_content, '2017')
소요시간: 10.86초 방식: where substring(wr_content,1,4)='2017'
소요시간: 10.87초 방식: where wr_content > '2017-00-00 00:00:00' and wr_content < '2018-00-00 00:00:00'
소요시간: 10.53초 방식: where wr_content between '2017-00-00 00:00:00' and '2018-00-00 00:00:00'
총 테이블수 79 / 총 레코드 :59,488 반복수 250
소요시간: 12.66초 방식: where wr_datetime like '%2017%'
소요시간: 15.75초 방식: where INSTR(wr_datetime, '2017')
소요시간: 10.06초 방식: where wr_datetime > '2017-00-00 00:00:00' and wr_datetime < '2018-00-00 00:00:00'
소요시간: 9.18초 방식: where wr_datetime between '2017-00-00 00:00:00' and '2018-00-00 00:00:00'
총 테이블수 79 / 총 레코드 :59,488 반복수 500
소요시간: 25.44초 방식: where wr_datetime like '%2017%'
소요시간: 31.45초 방식: where INSTR(wr_datetime, '2017')
소요시간: 20.04초 방식: where wr_datetime > '2017-00-00 00:00:00' and wr_datetime < '2018-00-00 00:00:00'
소요시간: 18.35초 방식: where wr_datetime between '2017-00-00 00:00:00' and '2018-00-00 00:00:00'
소스
<?php
include_once('./_common.php');
echo "<xmp>";
$limit=250;
echo "총 테이블수 79 / 총 레코드 :59,488 반복수 $limit \n";
$arr_time=array();
for ($i=0;$i<4;$i++) {
$start = get_time();
//echo microtime(),"\n";
for ($ii=0;$ii<$limit;$ii++) {
$sql="select * from {$g5['board_table']} where ";
$sql.="gr_id!='adm' ";
//$sql.="and bo_table not like '_%' ";
if ($g5['show']) echo "$sql\n";
$rst=sql_query($sql,true);
$count=0;
$count_total=0;
while ($board=sql_fetch_array($rst)) {
$bo_table=$board['bo_table'];
if (substr($bo_table,0,1)=='_') continue;
//if ($bo_table!='autocamping') continue;
//if ($bo_table!='hotucc') continue;
$write_table = $g5['write_prefix'] . $bo_table; // 게시판 테이블 전체이름
$tmp1 =sql_fetch("select count(*) as cnt from $write_table");
$sql2 ="select count(*) as cnt from $write_table where ";
//if ($i==0) $sql2.="wr_datetime like '2017%'";
if ($i==0) $sql2.="wr_datetime like '%2017%'";
if ($i==1) $sql2.="INSTR(wr_datetime, '2017')";
if ($i==2) $sql2.="wr_datetime > '2017-00-00 00:00:00' and wr_datetime < '2018-00-00 00:00:00' ";
if ($i==3) $sql2.="wr_datetime between '2017-00-00 00:00:00' and '2018-00-00 00:00:00' ";
$tmp2=sql_fetch($sql2,true);
$count++;
$count_total+=$tmp1['cnt'];
//echo "$bo_table -> {$tmp2['cnt']}\n";
}
}
$end = get_time();
//echo microtime(),"\n";
$time=$end - $start;
echo '소요시간: '.number_format($time,2)."초 방식: ";
if ($i==0) echo "where wr_datetime like '2017%'";
if ($i==1) echo "where INSTR(wr_datetime, '2017')";
if ($i==2) echo "where wr_datetime > '2017-00-00 00:00:00' and wr_datetime < '2018-00-00 00:00:00' ";
if ($i==3) echo "where wr_datetime between '2017-00-00 00:00:00' and '2018-00-00 00:00:00' ";
echo "\n";
}
function get_time() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
0
댓글 0개