E
E
Elena2015-12-01 07:45:12
Yii
Elena, 2015-12-01 07:45:12

How to optimize query to Yii2 database?

Faced the problem of a long page load associated with a heavy query to the database. I still can’t figure out how to solve it, in particular, because I still don’t understand well how AQ builds queries and AR builds objects based on them (well, I still don’t have experience in query optimization).
The bottom line is this: there are two related models (many-to-many relationship), and there is a third model, the data in which is set to link these two models (there are fields with the id of the first and second models). And when you need to get data from the third model to connect the first, it looks like this:

public function getNumbers()
  {
    return $this->hasMany(Number::className(), [
            'Entity1Id' => 'Entity1Id', 
            'Entity2Id' => 'Entity2Id'
        ]);
  }

And then you get this query:
SELECT * FROM `numbers` WHERE ((`Entity1Id`, `Entity2Id`) IN (('193', '773'), ('450', '773'), 
('453', '773'), ('193', '774'), ('450', '774'), ('453', '774'), ('193', '775'), ('450', '775'), ('453', '775'), 
('193', '776'), ('450', '776'), ('453', '776'), ('193', '777'), ('450', '777'), ('453', '777'), ('193', '778'), 
('450', '778'), ('453', '778'), ('193', '779'), ('450', '779'), ('453', '779'), ('193', '780'), ('450', '780'), 
('453', '780'), ('193', '781'), ('450', '781'), ('453', '781'), ('193', '782'), ('450', '782'), ('453', '782'), 
('193', '783'), ('450', '783'), ('453', '783'), ('193', '784'), ('450', '784'), ('453', '784'), ('193', '785'), 
('450', '785'), ('453', '785'), ('193', '786'), ('450', '786'), ('453', '786'), ('193', '787'), ('450', '787'), 
('453', '787'), ('193', '788'), ('450', '788'), ('453', '788'), ('193', '789'), ('450', '789'), ('453', '789'), 
('193', '790'), ('450', '790'), ('453', '790'), ('193', '791'), ('450', '791'), ('453', '791'), ('193', '792'), 
('450', '792'), ('453', '792'), ('193', '793'), ('450', '793'), ('453', '793'), ('193', '794'), ('450', '794'), 
('453', '794'), ('193', '795'), ('450', '795'), ('453', '795'), ('193', '796'), ('450', '796'), ('453', '796'), 
('193', '797'), ('450', '797'), ('453', '797'), ('193', '798'), ('450', '798'), ('453', '798'), ('193', '799'), 
('450', '799'), ('453', '799'), ('193', '800'), ('450', '800'), ('453', '800'), ('193', '801'), ('450', '801'), 
('453', '801'), ('193', '802'), ('450', '802'), ('453', '802'), ('193', '803'), ('450', '803'), ('453', '803'),
 ('193', '804'), ('450', '804'), ('453', '804'), ('193', '805'), ('450', '805'), ('453', '805'), ('193', '806'), 
('450', '806'), ('453', '806'), ('193', '807'), ('450', '807'), ('453', '807'), ('193', '808'), ('450', '808'), 
('453', '808'), ('193', '809'), ('450', '809'), ('453', '809'), ('193', '810'), ('450', '810'), ('453', '810'), 
('193', '811'), ('450', '811'), ('453', '811'), ('193', '812'), ('450', '812'), ('453', '812'), ('193', '813'), 
('450', '813'), ('453', '813'), ('193', '814'), ('450', '814'), ('453', '814'), ('193', '815'), ('450', '815'), 
('453', '815'), ('193', '816'), ('450', '816'), ('453', '816'), ('193', '817'), ('450', '817'), ('453', '817'), 
('193', '818'), ('450', '818'), ('453', '818'), ('193', '819'), ('450', '819'), ('453', '819'), ('193', '820'), 
('450', '820'), ('453', '820'), ('193', '821'), ('450', '821'), ('453', '821'), ('193', '822'), ('450', '822'), 
('453', '822'), ('193', '823'), ('450', '823'), ('453', '823'), ('193', '824'), ('450', '824'), ('453', '824'), 
('193', '825'), ('450', '825'), ('453', '825'), ('193', '826'), ('450', '826'), ('453', '826'), ('193', '827'), 
('450', '827'), ('453', '827'), ('193', '828'), ('450', '828'), ('453', '828'), ('193', '829'), ('450', '829'), 
('453', '829'), ('193', '830'), ('450', '830'), ('453', '830'), ('193', '831'), ('450', '831'), ('453', '831'), 
('193', '832'), ('450', '832'), ('453', '832'), ('193', '833'), ('450', '833'), ('453', '833'), ('193', '834'), 
('450', '834'), ('453', '834'), ('193', '835'), ('450', '835'), ('453', '835'), ('193', '836'), ('450', '836'), 
('453', '836'), ('193', '837'), ('450', '837'), ('453', '837'), ('193', '838'), ('450', '838'), ('453', '838'), 
('193', '839'), ('450', '839'), ('453', '839'), ('193', '840'), ('450', '840'), ('453', '840'), ('193', '841'), 
('450', '841'), ('453', '841'), ('193', '842'), ('450', '842'), ('453', '842'), ('193', '843'), ('450', '843'), 
('453', '843'), ('193', '844'), ('450', '844'), ('453', '844'), ('193', '845'), ('450', '845'), ('453', '845'), 
('193', '846'), ('450', '846'), ('453', '846'), ('193', '847'), ('450', '847'), ('453', '847'), ('193', '848'), 
('450', '848'), ('453', '848'), ('193', '849'), ('450', '849'), ('453', '849'), ('193', '850'), ('450', '850'), 
('453', '850'), ('193', '851'), ('450', '851'), ('453', '851'), ('193', '852'), ('450', '852'), ('453', '852'), 
('193', '853'), ('450', '853'), ('453', '853'), ('193', '854'), ('450', '854'), ('453', '854'), ('193', '855'), 
('450', '855'), ('453', '855'), ('193', '856'), ('450', '856'), ('453', '856'), ('193', '857'), ('450', '857'), 
('453', '857'), ('193', '858'), ('450', '858'), ('453', '858'), ('193', '859'), ('450', '859'), ('453', '859'), 
('193', '860'), ('450', '860'), ('453', '860'), ('193', '861'), ('450', '861'), ('453', '861'), ('193', '862'), 
('450', '862'), ('453', '862'), ('193', '863'), ('450', '863'), ('453', '863'), ('193', '864'), ('450', '864'), 
('453', '864'), ('193', '865'), ('450', '865'), ('453', '865'), ('193', '866'), ('450', '866'), ('453', '866'), 
('193', '867'), ('450', '867'), ('453', '867'), ('193', '868'), ('450', '868'), ('453', '868'), ('193', '869'), 
('450', '869'), ('453', '869'), ('193', '870'), ('450', '870'), ('453', '870'), ('193', '871'), ('450', '871'), 
('453', '871'), ('193', '872'), ('450', '872'), ('453', '872'), ('193', '873'), ('450', '873'), ('453', '873'), 
('193', '874'), ('450', '874'), ('453', '874'), ('193', '875'), ('450', '875'), ('453', '875'), ('193', '876'), 
('450', '876'), ('453', '876'), ('193', '877'), ('450', '877'), ('453', '877'), ('193', '878'), ('450', '878'), 
('453', '878'), ('193', '879'), ('450', '879'), ('453', '879'), ('193', '880'), ('450', '880'), ('453', '880'), 
('193', '881'), ('450', '881'), ('453', '881'), ('193', '882'), ('450', '882'), ('453', '882'), ('193', '883'), 
('450', '883'), ('453', '883'), ('193', '884'), ('450', '884'), ('453', '884'), ('193', '885'), ('450', '885'), 
('453', '885'), ('193', '886'), ('450', '886'), ('453', '886'), ('193', '887'), ('450', '887'), ('453', '887'), 
('193', '888'), ('450', '888'), ('453', '888'), ('193', '889'), ('450', '889'), ('453', '889'), ('193', '890'), 
('450', '890'), ('453', '890'), ('193', '891'), ('450', '891'), ('453', '891'), ('193', '892'), ('450', '892'), 
('453', '892'), ('193', '893'), ('450', '893'), ('453', '893'), ('193', '894'), ('450', '894'), ('453', '894'), 
('193', '895'), ('450', '895'), ('453', '895'), ('193', '896'), ('450', '896'), ('453', '896'), ('193', '897'), 
('450', '897'), ('453', '897'), ('193', '898'), ('450', '898'), ('453', '898'), ('193', '899'), ('450', '899'), 
('453', '899'), ('193', '900'), ('450', '900'), ('453', '900'), ('193', '901'), ('450', '901'), ('453', '901'), 
('193', '902'), ('450', '902'), ('453', '902'), ('193', '903'), ('450', '903'), ('453', '903'), ('193', '904'), 
('450', '904'), ('453', '904'), ('193', '905'), ('450', '905'), ('453', '905'), ('193', '906'), ('450', '906'), 
('453', '906'), ('193', '907'), ('450', '907'), ('453', '907'), ('193', '908'), ('450', '908'), ('453', '908'), 
('193', '909'), ('450', '909'), ('453', '909'), ('193', '910'), ('450', '910'), ('453', '910'), ('193', '911'), 
('450', '911'), ('453', '911'), ('193', '912'), ('450', '912'), ('453', '912'), ('193', '913'), ('450', '913'), 
('453', '913'), ('193', '914'), ('450', '914'), ('453', '914'), ('193', '915'), ('450', '915'), ('453', '915'), 
('193', '916'), ('450', '916'), ('453', '916'), ('193', '917'), ('450', '917'), ('453', '917'), ('193', '918'), 
('450', '918'), ('453', '918'), ('193', '919'), ('450', '919'), ('453', '919'), ('193', '920'), ('450', '920'), 
('453', '920'), ('193', '921'), ('450', '921'), ('453', '921'), ('193', '922'), ('450', '922'), ('453', '922'), 
('193', '923'), ('450', '923'), ('453', '923'))) ORDER BY `Date`

The first solution is to change the structure of the tables - to link data from the third table with the primary key of the table with the links of the first two models, but the problem is that this link can be deleted, then added again, its id will change, and the data should still be available .
Has anyone experienced this? What can be done here?
ZY:
the problem is not in writing the query and the structure of the tables, the problem is rather in the knowledge of Yii.
For some reason, his ORM makes several requests in a row, and then uses the WHERE IN construct several times in succession. I wrote a query in SQL language, it works 100 times faster. The problem is to embed it in the model so that it maps normally

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2015-12-01
@Rsa97

What does the EXPLAIN of a query show for using indexes?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question