douzhanrun0497 2014-05-21 06:07
浏览 54

使用cakephp从mysql数据库中获取大量数据

I have 7 similar tables named 'device_1_updates', 'device_2_updates', 'device_3_updates'...'device_7_updates'.

I am using UNION to face data from these 7 tables, But it is taking too much time .. since data in each of these 7 tables are too heavy.

Please suggest some way to make it fast

my sql query is:

SELECT 
  * 
FROM
  (SELECT 
    * 
  FROM
    device_1_updates 
  UNION
  SELECT 
    * 
  FROM
    device_1_updates 
  UNION
  SELECT 
    * 
  FROM
    device_2_updates 
  UNION
  SELECT 
    * 
  FROM
    device_3_updates 
  UNION
  SELECT 
    * 
  FROM
    device_4_updates 
  UNION
  SELECT 
    * 
  FROM
    device_5_updates 
  UNION
  SELECT 
    * 
  FROM
    device_6_updates 
  UNION
  SELECT 
    * 
  FROM
    device_7_updates) AS device_data 
WHERE device_time > '2014-04-05 01:55:08' 
  AND device_time < '2014-04-11 11:55:08' 
  AND imei = 357804045965906 
ORDER BY device_time ASC 
LIMIT 0, 100

and the corresponding response is:

"response": [{
        "device_data": {
            "imei": "357804045965906",
            "device_time": "2014-04-02 15:57:52",
            "lat": "",
            "lang": "",
            "event": "^6"
        }
    }, {
        "device_data": {
            "imei": "357804045965906",
            "device_time": "2014-04-02 15:58:02",
            "lat": "",
            "lang": "",
            "event": "^4"
        }
    }, {
        "device_data": {
            "imei": "357804045965906",
            "device_time": "2014-04-02 15:58:14",
            "lat": "",
            "lang": "",
            "event": "^8"
        }
    }, {
        "device_data": {
            "imei": "357804045965906",
            "device_time": "2014-04-02 15:58:19",
            "lat": "",
            "lang": "",
            "event": "^1"
        }
    }
]
  • 写回答

3条回答 默认 最新

  • dongshao4207 2014-05-21 06:13
    关注

    The easiest way to speed up that query would be to use UNION ALL instead of UNION. UNION will only return distinct values, meaning it has to check if they are distinct. UNION ALL returns everything, including duplicates. So if the tables don't contain duplicate data, use UNION ALL.

    To go further you would want to make sure you have proper indexes in place.

    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上