mysql两种查询方式对比--子查询与join查询

下面两个例子分别是:join查询与 子查询,网易的书上说 join查询要比 嵌套子查询要快,因为要用到临时表。。。

 

但我在mysql下执行结果却相反(如下),

 

Mysql 5.045

linux平台

 

 

mysql> explain SELECT  *  FROM material JOIN groupmaterial on  (material.id=groupmaterial.fid AND  groupmaterial.gid=1)  where (material.title like '%' OR material.tag LIKE '%' OR material.descript LIKE '%')  GROUP BY material.id  ORDER BY material.created ASC /G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: groupmaterial
         type: ref
possible_keys: groupmaterial_fid,groupmaterial_gid
          key: groupmaterial_gid
      key_len: 4
          ref: const
         rows: 17
        Extra: Using temporary; Using filesort
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: material
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 4
          ref: material.groupmaterial.fid
         rows: 1
        Extra: Using where
2 rows in set (0.00 sec)

ERROR:
No query specified

 

 

mysql> explain SELECT  *  FROM material WHERE id IN (SELECT DISTINCT fid FROM groupmaterial WHERE gid=1)  AND (material.title like '%' OR material.tag LIKE '%' OR material.descript LIKE '%')  GROUP BY id  ORDER BY created ASC /G;
*************************** 1. row ***************************
           id: 1
  select_type: PRIMARY
        table: material
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 30
        Extra: Using where; Using filesort
*************************** 2. row ***************************
           id: 2
  select_type: DEPENDENT SUBQUERY
        table: groupmaterial
         type: index_subquery
possible_keys: groupmaterial_fid,groupmaterial_gid
          key: groupmaterial_fid
      key_len: 4
          ref: func
         rows: 4
        Extra: Using where
2 rows in set (0.00 sec)

 

 

 

## 然后 写个脚本 在django环境下运行,两个语句 分别 执行 1000次。

 

[root@wps-node2 MaterialServer]# python2.5 test_mysqlcmd.py
1
search1 user time:  0:00:01.567074    # 这里是join查询
1
search2 user time:  0:00:01.432829    # 这里是子查询
[root@wps-node2 MaterialServer]# python2.5 test_mysqlcmd.py
1
search1 user time:  0:00:01.563794
1
search2 user time:  0:00:01.426497

 

 

 

脚本 内容如下:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#by TooNTonG at 2009/5/19

 

import settings
from django.core.management.__init__ import setup_environ
setup_environ(settings)

from django.db import connection
from django.db.models import Q
from datetime import datetime
import random


cursor = connection.cursor()
def search1(uname='', tag='', descript=''):

    sqlCmd = "SELECT * FROM material JOIN groupmaterial on  (material.id=groupmaterial.fid AND  groupmaterial.gid=1)  where (material.title like '%%"+ tag +"%%' OR material.tag LIKE '%%"+ tag+"%%' OR material.descript LIKE '%%" + tag+"%%')  GROUP BY material.id  ORDER BY material.created ASC"      #

 

    cursor.execute(sqlCmd)
    rs = cursor.fetchall()

    return rs

 

 

def search2(uname='', tag='', descript=''):
    sqlCmd = "SELECT * FROM material WHERE id IN (SELECT DISTINCT fid FROM groupmaterial WHERE gid=1)  AND (material.title like '%%"+tag+"%%' OR material.tag LIKE '%%"+tag+"%%' OR material.descript LIKE '%%"+tag+"%%')  GROUP BY id  ORDER BY created ASC "   #

    cursor.execute(sqlCmd)
    rs = cursor.fetchall()


    return rs

 

SUM = 1000
def main():

    user =1
    tag = 't'
    desc = 'd'

 

    begin = datetime.now()
    for i in range(SUM):
        search1(user, tag, desc)
    print len(search1(user, tag, desc))
    end = datetime.now()
    print "search1 user time: ", end - begin


    begin = datetime.now()
    for i in range(SUM):
        search2(user, tag, desc)
    print len(search2(user, tag, desc))
    end = datetime.now()
    print "search2 user time: ", end - begin

 


if __name__ == "__main__":
    main()
    pass

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值