Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.1
#60

[eluser]Anestetikas[/eluser]
Found a new problem to deal with.
I have a DB with folowing tables funds -< inventories -< files -< documents
All of these 4 tables has a many to many relations to languages and characteristics tables.

There are like 2k rows in funds 5k in inventories 4mil in files and ~200k in documents.
Every one of these rows has 0 1 or 2 relations to languages and characteristics.

I want to get all related languages and characteristics to specific fund, inventory, file.

If I do or_where_related:
Code:
$kalbos = new Language;
                $kalbos->select('description');
                $kalbos->or_where_related('inventory/fund', 'id', $fondas->id);
                $kalbos->or_where_related('file/inventory/fund', 'id', $fondas->id);
                $kalbos->or_where_related('document/file/inventory/fund', 'id', $fondas->id);
                $kalbos->group_by('id')->get();
                $this->data['kalbos'] = $kalbos;
it does a huge query:
Code:
SELECT  `languages`.`description`
FROM (
`languages`
)
LEFT OUTER JOIN  `inventories_languages` inventories_languages ON  `languages`.`id` =  `inventories_languages`.`language_id`
LEFT OUTER JOIN  `inventories` inventories ON  `inventories`.`id` =  `inventories_languages`.`inventory_id`
LEFT OUTER JOIN  `files_languages` files_languages ON  `languages`.`id` =  `files_languages`.`language_id`
LEFT OUTER JOIN  `files` files ON  `files`.`id` =  `files_languages`.`file_id`
LEFT OUTER JOIN  `inventories` file_inventories ON  `file_inventories`.`id` =  `files`.`inventory_id`
LEFT OUTER JOIN  `documents_languages` documents_languages ON  `languages`.`id` =  `documents_languages`.`language_id`
LEFT OUTER JOIN  `documents` documents ON  `documents`.`id` =  `documents_languages`.`document_id`
LEFT OUTER JOIN  `files` document_files ON  `document_files`.`id` =  `documents`.`file_id`
LEFT OUTER JOIN  `inventories` document_file_inventories ON  `document_file_inventories`.`id` =  `document_files`.`inventory_id`
WHERE  `inventories`.`fund_id` =831
OR  `file_inventories`.`fund_id` =831
OR  `document_file_inventories`.`fund_id` =831
GROUP BY  `languages`.`id`
EXPLAIN:
Code:
+----+-------------+---------------------------+--------+------------------------------+------------------+---------+----------------------------------------------+------+-------------+
| id | select_type | table                     | type   | possible_keys                | key              | key_len | ref                                          | rows | Extra       |
+----+-------------+---------------------------+--------+------------------------------+------------------+---------+----------------------------------------------+------+-------------+
|  1 | SIMPLE      | languages                 | index  | NULL                         | PRIMARY          | 2       | NULL                                         |    1 |             |
|  1 | SIMPLE      | inventories_languages     | ref    | lan_inv_unique,language_id   | lan_inv_unique   | 2       | pagrindas.languages.id                       |   59 | Using index |
|  1 | SIMPLE      | inventories               | eq_ref | PRIMARY                      | PRIMARY          | 2       | pagrindas.inventories_languages.inventory_id |    1 |             |
|  1 | SIMPLE      | files_languages           | ref    | lang_file_unique,language_id | lang_file_unique | 3       | pagrindas.languages.id                       |    1 | Using index |
|  1 | SIMPLE      | files                     | eq_ref | PRIMARY                      | PRIMARY          | 4       | pagrindas.files_languages.file_id            |    1 |             |
|  1 | SIMPLE      | file_inventories          | eq_ref | PRIMARY                      | PRIMARY          | 2       | pagrindas.files.inventory_id                 |    1 |             |
|  1 | SIMPLE      | documents_languages       | ref    | lang_doc_unique,language_id  | lang_doc_unique  | 3       | pagrindas.languages.id                       |  525 | Using index |
|  1 | SIMPLE      | documents                 | eq_ref | PRIMARY                      | PRIMARY          | 4       | pagrindas.documents_languages.document_id    |    1 |             |
|  1 | SIMPLE      | document_files            | eq_ref | PRIMARY                      | PRIMARY          | 4       | pagrindas.documents.file_id                  |    1 |             |
|  1 | SIMPLE      | document_file_inventories | eq_ref | PRIMARY                      | PRIMARY          | 2       | pagrindas.document_files.inventory_id        |    1 | Using where |
+----+-------------+---------------------------+--------+------------------------------+------------------+---------+----------------------------------------------+------+-------------+
From EXPLAIN we can see that it creates 3 temp tables document_files, document_file_inventories and file_inventories.
Query execution:
Code:
34 rows in set (24 min 18.99 sec)

Is there a way to get related languages for each object (its very fast), and then merge them into one $languages object?

EDIT:
Went around with getting ids all_to_array for each object, then merged ids to $langIDs array, then selected once again by where_in('id', $langIDs); Lots of code, but seems to get proper stuff in 0.04sec instead of 25minutes.


Messages In This Thread
DataMapper ORM v1.8.1 - by El Forum - 06-07-2011, 12:37 PM
DataMapper ORM v1.8.1 - by El Forum - 06-07-2011, 01:37 PM
DataMapper ORM v1.8.1 - by El Forum - 06-07-2011, 09:09 PM
DataMapper ORM v1.8.1 - by El Forum - 06-08-2011, 12:29 AM
DataMapper ORM v1.8.1 - by El Forum - 06-08-2011, 03:35 AM
DataMapper ORM v1.8.1 - by El Forum - 06-08-2011, 07:29 AM
DataMapper ORM v1.8.1 - by El Forum - 06-08-2011, 08:30 AM
DataMapper ORM v1.8.1 - by El Forum - 06-08-2011, 09:42 AM
DataMapper ORM v1.8.1 - by El Forum - 06-08-2011, 10:35 AM
DataMapper ORM v1.8.1 - by El Forum - 06-08-2011, 10:40 AM
DataMapper ORM v1.8.1 - by El Forum - 06-08-2011, 10:44 AM
DataMapper ORM v1.8.1 - by El Forum - 06-08-2011, 04:48 PM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 01:05 AM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 01:15 AM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 01:21 AM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 01:21 AM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 01:32 AM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 01:41 AM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 01:50 AM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 02:25 AM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 04:15 AM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 04:23 AM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 06:46 AM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 06:47 AM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 08:10 AM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 08:16 AM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 08:37 AM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 05:42 PM
DataMapper ORM v1.8.1 - by El Forum - 06-09-2011, 07:45 PM
DataMapper ORM v1.8.1 - by El Forum - 06-10-2011, 12:41 AM
DataMapper ORM v1.8.1 - by El Forum - 06-10-2011, 03:05 AM
DataMapper ORM v1.8.1 - by El Forum - 06-10-2011, 09:54 AM
DataMapper ORM v1.8.1 - by El Forum - 06-11-2011, 04:34 AM
DataMapper ORM v1.8.1 - by El Forum - 06-11-2011, 04:39 AM
DataMapper ORM v1.8.1 - by El Forum - 06-13-2011, 04:03 AM
DataMapper ORM v1.8.1 - by El Forum - 06-13-2011, 04:11 AM
DataMapper ORM v1.8.1 - by El Forum - 06-13-2011, 07:55 AM
DataMapper ORM v1.8.1 - by El Forum - 06-13-2011, 08:29 AM
DataMapper ORM v1.8.1 - by El Forum - 06-13-2011, 04:08 PM
DataMapper ORM v1.8.1 - by El Forum - 06-13-2011, 04:13 PM
DataMapper ORM v1.8.1 - by El Forum - 06-13-2011, 06:55 PM
DataMapper ORM v1.8.1 - by El Forum - 06-13-2011, 08:19 PM
DataMapper ORM v1.8.1 - by El Forum - 06-14-2011, 02:01 AM
DataMapper ORM v1.8.1 - by El Forum - 06-14-2011, 07:57 AM
DataMapper ORM v1.8.1 - by El Forum - 06-14-2011, 07:58 AM
DataMapper ORM v1.8.1 - by El Forum - 06-14-2011, 08:06 AM
DataMapper ORM v1.8.1 - by El Forum - 06-15-2011, 01:53 AM
DataMapper ORM v1.8.1 - by El Forum - 06-15-2011, 01:59 AM
DataMapper ORM v1.8.1 - by El Forum - 06-15-2011, 02:29 AM
DataMapper ORM v1.8.1 - by El Forum - 06-15-2011, 02:50 AM
DataMapper ORM v1.8.1 - by El Forum - 06-15-2011, 06:22 AM
DataMapper ORM v1.8.1 - by El Forum - 06-15-2011, 07:26 AM
DataMapper ORM v1.8.1 - by El Forum - 06-15-2011, 09:11 AM
DataMapper ORM v1.8.1 - by El Forum - 06-15-2011, 02:19 PM
DataMapper ORM v1.8.1 - by El Forum - 06-15-2011, 08:31 PM
DataMapper ORM v1.8.1 - by El Forum - 06-16-2011, 12:37 AM
DataMapper ORM v1.8.1 - by El Forum - 06-16-2011, 01:21 AM
DataMapper ORM v1.8.1 - by El Forum - 06-21-2011, 12:09 PM
DataMapper ORM v1.8.1 - by El Forum - 06-21-2011, 01:04 PM
DataMapper ORM v1.8.1 - by El Forum - 06-22-2011, 01:19 AM
DataMapper ORM v1.8.1 - by El Forum - 06-22-2011, 02:25 AM
DataMapper ORM v1.8.1 - by El Forum - 06-22-2011, 03:59 AM
DataMapper ORM v1.8.1 - by El Forum - 06-22-2011, 04:18 AM
DataMapper ORM v1.8.1 - by El Forum - 06-22-2011, 10:53 AM
DataMapper ORM v1.8.1 - by El Forum - 06-22-2011, 12:00 PM
DataMapper ORM v1.8.1 - by El Forum - 06-24-2011, 07:04 AM
DataMapper ORM v1.8.1 - by El Forum - 06-24-2011, 09:46 AM
DataMapper ORM v1.8.1 - by El Forum - 06-24-2011, 11:41 AM
DataMapper ORM v1.8.1 - by El Forum - 06-24-2011, 12:27 PM
DataMapper ORM v1.8.1 - by El Forum - 06-24-2011, 03:16 PM
DataMapper ORM v1.8.1 - by El Forum - 06-25-2011, 03:32 AM
DataMapper ORM v1.8.1 - by El Forum - 06-25-2011, 12:47 PM
DataMapper ORM v1.8.1 - by El Forum - 06-25-2011, 01:39 PM
DataMapper ORM v1.8.1 - by El Forum - 06-25-2011, 04:07 PM
DataMapper ORM v1.8.1 - by El Forum - 06-27-2011, 03:11 AM
DataMapper ORM v1.8.1 - by El Forum - 06-27-2011, 04:44 AM
DataMapper ORM v1.8.1 - by El Forum - 06-27-2011, 04:55 AM
DataMapper ORM v1.8.1 - by El Forum - 06-27-2011, 05:23 AM
DataMapper ORM v1.8.1 - by El Forum - 06-27-2011, 07:16 AM
DataMapper ORM v1.8.1 - by El Forum - 06-27-2011, 08:55 AM
DataMapper ORM v1.8.1 - by El Forum - 06-27-2011, 08:56 AM
DataMapper ORM v1.8.1 - by El Forum - 06-30-2011, 04:37 AM
DataMapper ORM v1.8.1 - by El Forum - 06-30-2011, 05:59 AM
DataMapper ORM v1.8.1 - by El Forum - 06-30-2011, 06:25 AM
DataMapper ORM v1.8.1 - by El Forum - 06-30-2011, 07:55 AM
DataMapper ORM v1.8.1 - by El Forum - 06-30-2011, 08:14 AM
DataMapper ORM v1.8.1 - by El Forum - 06-30-2011, 03:26 PM
DataMapper ORM v1.8.1 - by El Forum - 06-30-2011, 04:43 PM
DataMapper ORM v1.8.1 - by El Forum - 06-30-2011, 11:11 PM
DataMapper ORM v1.8.1 - by El Forum - 07-04-2011, 08:38 AM
DataMapper ORM v1.8.1 - by El Forum - 07-04-2011, 08:48 AM
DataMapper ORM v1.8.1 - by El Forum - 07-04-2011, 08:55 AM
DataMapper ORM v1.8.1 - by El Forum - 07-04-2011, 08:06 PM
DataMapper ORM v1.8.1 - by El Forum - 07-05-2011, 12:33 AM
DataMapper ORM v1.8.1 - by El Forum - 07-05-2011, 03:22 PM
DataMapper ORM v1.8.1 - by El Forum - 07-06-2011, 02:26 AM
DataMapper ORM v1.8.1 - by El Forum - 07-06-2011, 03:08 AM
DataMapper ORM v1.8.1 - by El Forum - 07-06-2011, 03:25 AM
DataMapper ORM v1.8.1 - by El Forum - 07-06-2011, 03:55 AM
DataMapper ORM v1.8.1 - by El Forum - 07-12-2011, 02:55 AM
DataMapper ORM v1.8.1 - by El Forum - 07-12-2011, 04:13 AM
DataMapper ORM v1.8.1 - by El Forum - 07-12-2011, 04:43 AM
DataMapper ORM v1.8.1 - by El Forum - 07-12-2011, 05:02 PM
DataMapper ORM v1.8.1 - by El Forum - 07-12-2011, 05:14 PM
DataMapper ORM v1.8.1 - by El Forum - 07-13-2011, 01:58 AM
DataMapper ORM v1.8.1 - by El Forum - 07-13-2011, 02:01 AM
DataMapper ORM v1.8.1 - by El Forum - 07-13-2011, 02:04 AM
DataMapper ORM v1.8.1 - by El Forum - 07-13-2011, 02:14 AM
DataMapper ORM v1.8.1 - by El Forum - 07-13-2011, 02:16 AM
DataMapper ORM v1.8.1 - by El Forum - 07-13-2011, 02:18 AM
DataMapper ORM v1.8.1 - by El Forum - 07-13-2011, 08:45 AM
DataMapper ORM v1.8.1 - by El Forum - 07-13-2011, 01:02 PM
DataMapper ORM v1.8.1 - by El Forum - 07-13-2011, 03:40 PM
DataMapper ORM v1.8.1 - by El Forum - 07-13-2011, 04:51 PM
DataMapper ORM v1.8.1 - by El Forum - 07-14-2011, 02:09 AM
DataMapper ORM v1.8.1 - by El Forum - 07-20-2011, 12:43 PM
DataMapper ORM v1.8.1 - by El Forum - 07-21-2011, 04:53 AM
DataMapper ORM v1.8.1 - by El Forum - 07-21-2011, 05:42 AM
DataMapper ORM v1.8.1 - by El Forum - 08-03-2011, 05:03 PM
DataMapper ORM v1.8.1 - by El Forum - 08-03-2011, 07:19 PM
DataMapper ORM v1.8.1 - by El Forum - 08-04-2011, 02:33 AM
DataMapper ORM v1.8.1 - by El Forum - 08-08-2011, 09:37 AM
DataMapper ORM v1.8.1 - by El Forum - 08-09-2011, 08:46 PM
DataMapper ORM v1.8.1 - by El Forum - 01-23-2012, 03:04 AM
DataMapper ORM v1.8.1 - by El Forum - 01-23-2012, 07:29 AM
DataMapper ORM v1.8.1 - by El Forum - 01-23-2012, 08:14 AM
DataMapper ORM v1.8.1 - by El Forum - 01-23-2012, 09:30 AM
DataMapper ORM v1.8.1 - by El Forum - 01-23-2012, 10:34 AM
DataMapper ORM v1.8.1 - by El Forum - 01-23-2012, 02:20 PM
DataMapper ORM v1.8.1 - by El Forum - 01-24-2012, 08:22 AM
DataMapper ORM v1.8.1 - by El Forum - 01-26-2012, 04:48 AM
DataMapper ORM v1.8.1 - by El Forum - 01-26-2012, 06:03 AM
DataMapper ORM v1.8.1 - by El Forum - 01-26-2012, 09:12 AM
DataMapper ORM v1.8.1 - by El Forum - 01-26-2012, 09:36 AM
DataMapper ORM v1.8.1 - by El Forum - 01-26-2012, 10:56 AM
DataMapper ORM v1.8.1 - by El Forum - 01-26-2012, 06:23 PM
DataMapper ORM v1.8.1 - by El Forum - 01-27-2012, 03:11 AM
DataMapper ORM v1.8.1 - by El Forum - 02-19-2012, 02:40 PM
DataMapper ORM v1.8.1 - by El Forum - 02-20-2012, 02:04 AM
DataMapper ORM v1.8.1 - by El Forum - 02-24-2012, 10:13 AM
DataMapper ORM v1.8.1 - by El Forum - 02-24-2012, 01:49 PM
DataMapper ORM v1.8.1 - by El Forum - 02-24-2012, 02:33 PM
DataMapper ORM v1.8.1 - by El Forum - 02-25-2012, 04:43 AM
DataMapper ORM v1.8.1 - by El Forum - 02-27-2012, 05:11 AM
DataMapper ORM v1.8.1 - by El Forum - 02-27-2012, 06:31 AM
DataMapper ORM v1.8.1 - by El Forum - 02-27-2012, 03:24 PM
DataMapper ORM v1.8.1 - by El Forum - 02-28-2012, 05:24 AM
DataMapper ORM v1.8.1 - by El Forum - 02-28-2012, 07:55 AM
DataMapper ORM v1.8.1 - by El Forum - 02-28-2012, 08:14 AM
DataMapper ORM v1.8.1 - by El Forum - 02-29-2012, 04:07 AM
DataMapper ORM v1.8.1 - by El Forum - 03-01-2012, 08:09 AM
DataMapper ORM v1.8.1 - by El Forum - 03-01-2012, 10:11 AM
DataMapper ORM v1.8.1 - by El Forum - 04-09-2012, 08:50 AM
DataMapper ORM v1.8.1 - by El Forum - 04-09-2012, 09:53 AM
DataMapper ORM v1.8.1 - by El Forum - 04-09-2012, 01:51 PM
DataMapper ORM v1.8.1 - by El Forum - 04-09-2012, 02:49 PM
DataMapper ORM v1.8.1 - by El Forum - 04-10-2012, 12:33 AM
DataMapper ORM v1.8.1 - by El Forum - 04-10-2012, 02:47 AM
DataMapper ORM v1.8.1 - by El Forum - 04-10-2012, 03:20 AM
DataMapper ORM v1.8.1 - by El Forum - 04-11-2012, 07:06 AM
DataMapper ORM v1.8.1 - by El Forum - 05-15-2012, 02:40 AM
DataMapper ORM v1.8.1 - by El Forum - 05-15-2012, 02:47 AM
DataMapper ORM v1.8.1 - by El Forum - 01-14-2013, 01:16 PM
DataMapper ORM v1.8.1 - by El Forum - 01-21-2013, 05:54 AM
DataMapper ORM v1.8.1 - by El Forum - 03-04-2013, 08:52 PM
DataMapper ORM v1.8.1 - by El Forum - 03-05-2013, 08:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB