douzhuang1900 2016-12-29 08:52
浏览 10

使用多个表的优化方式查询结果

I am trying to get data using 4 tables. List of Tables

  • numbers
  • calls
  • ref_label_no
  • labels

I want to fetch data no of calls's Number attached with specific labels. Here is table.
Number Table :

select id,uuid,number from numbers limit 1;
+----+--------------------------------------+------------+
| id | uuid                                 | number     |
+----+--------------------------------------+------------+
|  1 | ed268b05-758e-44fd-b429-8d5223651814 | 1234561222 |
+----+--------------------------------------+------------+

Call Table

mysql> select id,uuid,did from calls limit 2;
+----+-------------------------------------+------------+
| id | uuid                                | number     |
+----+-------------------------------------+------------+
|  1 | ddddass-b810-4f23-7456-8ff56efab080 | 1234561222 |
|  2 | dddddd-b810-4f23-7456-8ff56efab080  | 123456789  |
+----+-------------------------------------+------------+
2 rows in set (0.02 sec)

ref_label_no Table

mysql> select uuid,number_uuid,label_uuid from ref_label_no limit 1;
+--------------------------------------+--------------------------------------+--------------------------------------+
| uuid                                 | number_uuid                          | label_uuid                           |
+--------------------------------------+--------------------------------------+--------------------------------------+
| 7a5e1b6e-0194-4993-8d78-5f7a1b60c7d2 | ed268b05-758e-44fd-b429-8d5223651814 | 7262b06e-9263-4825-8411-4c107104a60b |
+--------------------------------------+--------------------------------------+--------------------------------------+
1 row in set (0.00 sec)

labels table

mysql> select uuid,name from labels limit 1;
+--------------------------------------+-------+
| uuid                                 | name  |
+--------------------------------------+-------+
| 7262b06e-9263-4825-8411-4c107104a60b | INDIA |
+--------------------------------------+-------+
1 row in set (0.00 sec)

Now I want result like Calls number with total calls.[This Output i needed]

mysql> select uuid,name from labels limit 1;
+--------------------------------------+-------+
| Number                       | Total_calls  |
+--------------------------------------+-------+
| 13456789                     |       2      |
+--------------------------------------+-------+
1 row in set (0.00 sec)

I have made below query but no success also taking too much time to response.

select count(*) as Total_calls,calls.number as did,labels.name,calls.created_at as callstart from calls, labels JOIN ref_label_no ON labels.uuid = ref_label_no.label_uuid JOIN numbers ON numbers.uuid = ref_label_no.number_uuid where labels.name="INDIA"  group by calls.number;

Any Suggestion Or recommendation ?

  • 写回答

2条回答 默认 最新

  • doupu7651 2016-12-29 09:02
    关注

    Multiple tables Avoid using join, use Union. It will be faster than join.

    Like this,

    select uuid,did from calls union 
    select uuid,number_uuid as did from ref_label_no union
    select uuid,name as did from labels
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算