Mysql Query运行速度很慢

时间:2015-06-08 00:48:55

标签: mysql

我有一个带有 7表的Mysql数据库。我的问题是数据库中有数百万条记录,以下查询需要花费大量时间。我该怎么办?

(SELECT 'res' AS TBL
      , mlsnum
      , streetname
      , streetnum
      , listdate
      , gpext_latitude
      , gpext_longitude
      , zipcode
      , listprice
      , beds
      , bathsfull
      , sqftbldg
      , modified
      , yearbuilt
   FROM rets_property_res
  WHERE liststatus = "active")
  UNION 
    ALL
(SELECT 'rnt' AS TBL
      , mlsnum
      , streetname
      , streetnum
      , listdate
      , gpext_latitude
      , gpext_longitude
      , zipcode
      , listprice
      , beds
      , bathsfull
      , sqftbldg
      , modified
      , yearbuilt
   FROM rets_property_rnt
  WHERE liststatus = "active")
  UNION  
    ALL
(SELECT 'lnd' AS TBL
      , mlsnum
      , streetname
      , streetnum
      , listdate
      , gpext_latitude
      , gpext_longitude
      , zipcode
      , listprice
      , NULL AS BEDS
      , NULL AS BATHSFULL
      , NULL AS SQFTBLDG
      , modified
      , NULL AS YEARBUILT
   FROM rets_property_lnd
  WHERE liststatus = "active")
  UNION
    ALL
(SELECT 'hir' AS TBL
      , mlsnum
      , streetname
      , streetnum
      , listdate
      , gpext_latitude
      , gpext_longitude
      , zipcode
      , listprice
      , beds
      , bathsfull
      , sqftbldg
      , modified
      , yearbuilt
   FROM rets_property_hir
  WHERE liststatus = "active")
  UNION
    ALL
(SELECT 'cnd' AS TBL
      , mlsnum
      , streetname
      , streetnum
      , listdate
      , gpext_latitude
      , gpext_longitude
      , zipcode
      , listprice
      , beds
      , bathsfull
      , sqftbldg
      , modified
      , yearbuilt
   FROM rets_property_cnd
  WHERE liststatus = "active")
  UNION 
    ALL
(SELECT 'mul' AS TBL
      , mlsnum
      , streetname
      , streetnum
      , listdate
      , gpext_latitude
      , gpext_longitude
      , zipcode
      , listprice
      , NULL AS BEDS
      , NULL AS BATHSFULL
      , sqftbldg
      , modified
      , yearbuilt
   FROM rets_property_mul
  WHERE liststatus = "active")
  ORDER 
     BY listdate DESC
  LIMIT 0, 48 

1 个答案:

答案 0 :(得分:2)

所有查询都不同。尝试将UNION替换为UNION ALL。不必删除重复项应该可以节省很多。

为了节省更多费用,请将每个子查询限制为48行,并将其标记为:

SELECT 'res' AS TBL, . . .
FROM rets_property_res
WHERE liststatus = 'active'
ORDER BY listdate DESC
LIMIT 48

在每个表和(liststatus, listdate)上都有索引,查询可能会变得非常快。