简体   繁体   中英

Too many queries per page

I'm using Yii2 and Yii2's standard way to fetch data from database. Using also JOINs, it takes 23 queries to fetch 90 rows + some other stuff loaded with the page.

But when I start showing data in table, it becomes critical. Every row has multiple pictures and I take only one picture to show. That's when number of queries go above 100.

This is how I fetch that one image:

Table1::find()->where(['IDauction'=>$ID])->limit(1)->orderBy('ID ASC')->one();

Very simple query, but when I have 90 rows and each row execute this query it goes above 100 per page. I'm not sure how to fix this, because when I show all data using relations number of queries go above 500. For example I fetch user's profile image also and that image is in different table.

This is how I do it currently

$query = Auction::find();
$query->joinWith(['relationIDpigeon', 'relationIDuser']);
$dataProvider = new ActiveDataProvider([
    'query' => $query,
    'sort'=> ['defaultOrder' => ['start_time'=>SORT_DESC]],//newest first
    'pagination'=>
    [
        'pageSize'=>100,
    ]
]);

Your query isn't written correctly. Also you should set up your relations in Yii. That way you can reference related tables and row.

Your query should look like this

$result = Table1::model()->findAllByAttributes(array('IDauction' =>$ID),array('order'=>'ID ASC','limit'=> 1));

And referencing related fields like this

$related_rows = $result->related_rows;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM