MySQL JOIN和SUBSELECT CONCAT

时间:2015-12-16 16:42:26

标签: php mysql database

作为一名开发人员,我知道一个优秀的数据库人物非常重视黄金。我经常发现自己使用非常低效的方法来解决非关键问题,但在目前的情况下,我需要速度超过“只是让它工作”。我甚至不会展示我所做的事情,因为它令人尴尬,但我只想说我在我的主要PHP循环中有子查询。遗憾。

我有几个表需要连接在一起,以便为索引引擎(Apache SOLR)提供json请求。

tbl_contacts
+----+--------------+---------------+
| ID | FirstName    | LastName      |
+----+--------------+---------------+
| 1  | Joe          | Blogs         |
+----+--------------+---------------+
| 2  | Jane         | Baker         |
+----+--------------+---------------+
| 3  | John         | Doe           |
+----+--------------+---------------+


tbl_attributes_map
+----+--------------+---------------+
| ID | ContactID    | AttributeID   |
+----+--------------+---------------+
| 1  | 1            | 1             |
+----+--------------+---------------+
| 2  | 1            | 3             |
+----+--------------+---------------+
| 3  | 2            | 2             |
+----+--------------+---------------+


tbl_attributes
+----+---------------+---------------+
| ID | AttributeType | Attribute     |
+----+---------------+---------------+
| 1  | Lower         | Shoe          |
+----+---------------+---------------+
| 2  | Upper         | T-Shirt       |
+----+---------------+---------------+
| 3  | Upper         | Vest          |
+----+---------------+---------------+


tbl_notes
+----+---------------+---------------+
| ID | ContactID     | Note          |
+----+---------------+---------------+
| 1  | 1             | Big feet      |
+----+---------------+---------------+
| 2  | 2             | Showoff       |
+----+---------------+---------------+
| 3  | 2             | Sweaty        |
+----+---------------+---------------+

tbl_appointment
+----+---------------+---------------+--------------------+-------------------+
| ID | ContactID     | Location      | TimeFrom           | TimeTo            |
+----+---------------+---------------+--------------------+-------------------+
| 1  | 1             | Big Ben       | 2015-12-16 08:00:00|2015-12-16 08:30:00|
+----+---------------+---------------+--------------------+-------------------+
| 1  | 2             | London        | 2015-12-17 10:00:00|2015-12-17 11:00:00|
+----+---------------+---------------+--------------------+-------------------+
| 1  | 2             | New York      | 2015-12-16 12:00:00|2015-12-16 12:30:00|
+----+---------------+---------------+--------------------+-------------------+

我需要运行一个查询,它基本上允许我打印一个数组结构,如:

Array(
   [FirstName]   => Joe
   [LastName]    => Blogs
   [Upper]       => Array(
                       Vest
                    )
   [Lower]       => Array(
                       Shoe
                    )
   [Notes]       => Array(
                       Big Feet
                    )
   [Location]    => Array(
                       Big Ben
                    )
   [ApptFrom]    => Array(
                       2015-12-16 08:00:00
                    )
   [ApptTo]      => Array(
                       2015-12-16 08:30:00
                    )

如果我可以进入一个可以运行查询以获得以下输出的阶段,我可以运行一个分隔符来破坏我需要为其构建数组的字段。 EG:

+----+------------+-----------+---------+---------+-----------------+-------------------+-----------------------------------------+-----------------------------------------+
| ID | FirstName  | LastName  | Upper   | Lower   | Notes           | Location          | ApptFrom                                | ApptTo                                  |
+----+------------+-----------+---------+---------+-----------------+-------------------+-----------------------------------------+-----------------------------------------+
| 2  | Jane       | Baker     | T-Shirt |         | Show off,Sweaty | London,New York   | 2015-12-17 10:00:00,2015-12-16 12:00:00 | 2015-12-17 11:00:00,2015-12-16 12:30:00 |
+----+------------+-----------+---------+---------+-----------------+-------------------+-----------------------------------------+-----------------------------------------+

我的脚本目前有效,但性能很高。通过80,000个联系人流失大约需要3个小时: - /

提前致谢。

1 个答案:

答案 0 :(得分:0)

当我开始索引Elasticsearch(使用Apache Lucene作为SOLR)5000万个数据时,我的情况完全相同,现在我只需要几个小时,我认为你可以做的是:

  1. 对您的查询进行解释,以查看您的查询是否使用了正确的索引
  2. 尝试使用更多的sub select而不是join,因为mysql有数百万数据的索引选择问题(你可以使用force index)
  3. 启动多线程来进行索引