简体   繁体   中英

Mysql Query running very slow

I have a Mysql database with 7 tables .My problem is there are millions of records in database, the following query is taking lot of time.what should i do?

(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 

All the queries are different. Try replacing the UNION with UNION ALL . Not having to remove duplicates should be a big savings.

For more savings, then limit each subquery to 48 rows and phrase them as:

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

With an index on each table and (liststatus, listdate) , the query might become blindingly fast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM