I have the select like this:
StoreProduct::where('user_id', Auth::user()->main_id)
->whereNotExists(function($query)
{
$query->select(DB::raw(1))
->from('store_products')
->whereRaw('store_products.store_product_id = store_product.id');
})
->orderBy('id', 'desc')->paginate($perPage)
In my database are 20K rows, and this select execute about 65 seconds.
Im using laravel debugbar. Queries dump 2 select:
select count(*) as aggregate from `store_product` where `store_product`.`deleted_at` is null and `user_id` = '20' and not exists (select 1 from `store_products` where store_products.store_product_id = store_product.id)65.88s
select * from `store_product` where `store_product`.`deleted_at` is null and `user_id` = '20' and not exists (select 1 from `store_products` where store_products.store_product_id = store_product.id) order by `id` desc limit 24 offset 0
Why laravel do select count(*) as aggregate? How to optimizate this query?
Related
how I can do this
Ex: select 5 random rows from the 20 most recent row
I need to do this with eloquent and get data with a query from DB not with mapping data
I found this query but I can't convert this to laravel eloquent …
Query :
select tbl1.* from (select *from DemoTable ORDER BY ShippingDate DESC LIMIT 20 ) as tbl1
-> ORDER BY RAND() LIMIT 5;
you can use this code. Change Models and columns for your models and columns. I wrote a user for the test
return User::query()
->whereIn('id', function($query)
{
$query->from('users')
->selectRaw('id')
->orderByDesc('created_at')->limit(20);
})->inRandomOrder()->limit(5)->get();
if you get toSql() you see
select * from `users` where `id` in (select id from `users` order by `created_at` desc limit 20) limit 5
When I run single exist query I get results as expected:
SELECT *
FROM `slasher_farming_mods`
WHERE EXISTS (SELECT * FROM `slasher_farming_brands`
WHERE `slasher_farming_mods`.`brand_id` = `slasher_farming_brands`.`id`
AND `brand_id` = '7'
ORDER BY `id` asc)
LIMIT 4 OFFSET 0
When I run multiple exist queries, I don't get any results:
SELECT *
FROM `slasher_farming_mods`
WHERE EXISTS (SELECT * FROM `slasher_farming_brands`
WHERE `slasher_farming_mods`.`brand_id` = `slasher_farming_brands`.`id`
AND `brand_id` = '7'
ORDER BY `id` ASC)
AND EXISTS (SELECT * FROM `slasher_farming_brands`
WHERE `slasher_farming_mods`.`brand_id` = `slasher_farming_brands`.`id`
AND `brand_id` = '24'
ORDER BY `id` ASC)
LIMIT 4 OFFSET 0
Tried using debugbar in laravel to see if my query is taking too long, but it takes less than 1ms. What could be wrong here, I tried to run these query also directly inside phpmyadmin but still no results with more than one where exists.
Query is populated by foreach loop in laravel.
foreach ($brands as $brand){
$query->whereHas('brand', function($q) use ($brand){
$q->where('brand_id', '=', $brand)->orderBy('id');
});
}
}
Your query doesn't work because brand_id cannot be both 7 and 24 for a given record. You want "OR". It would be clearer to just use the expression brand_id in (7, 24) instead of the two separate sub-queries. There is no point in sorting the sub-query.
Alternatively, join the two tables:
select ...
from slasher_farming_mods m
join slasher_farming_brands b on m.brand_id = b.id
where brand_id in (7, 24)
LIMIT 4 OFFSET 0
I want result in a single query.
Currently, I am using 2 queries. First for the distinct rows and the second is for the count rows. Second query fire for each row of the first query. Which is not good for the server.
I tried it in a single query but it shows count as 1 for each row which is wrong. I don't know how to do it in a single query.
Please help!
2 queries:
SELECT DISTINCT `_car_make`.`id` as `make_id`, `_car_make`.`title`
FROM `_motor`, `_car_make`
WHERE `_motor`.`make` = `_car_make`.`id`
AND `domain` = 'domain.com' AND `active` = 'y' AND `_motor`.`status` != 'SOLD'
GROUP BY `_motor`.`id` ORDER BY `_car_make`.`title`
SELECT COUNT(DISTINCT `id`) AS `TOTAL_MAKE` FROM `_motor`
WHERE `_motor`.`make` = '$make_id'
AND `domain` = 'domain.com' AND `_motor`.`status` != 'SOLD' AND `active` = 'y'
I tried it in 1 query, which shows count as 1 which is wrong:
SELECT DISTINCT `_car_make`.`id` as `make_id`, `_car_make`.`title`, count(`_motor`.`id`) AS TotalMake
FROM `_motor`, `_car_make`
WHERE `_motor`.`make` = `_car_make`.`id`
AND `domain` = 'domain.com' AND `active` = 'y' AND `_motor`.`status` != 'SOLD'
GROUP BY `_motor`.`id` ORDER BY `_car_make`.`title`
You can try below-
SELECT `_car_make`.`id` as `make_id`, `_car_make`.`title`, count(*) AS TotalMake
FROM `_motor` inner join `_car_make`
on `_motor`.`make` = `_car_make`.`id`
AND `domain` = 'coxheathcarcentre.co.uk'
where `active` = 'y' AND `_motor`.`status` != 'SOLD'
GROUP BY `_car_make`.`id`,`_car_make`.`title`
ORDER BY `_car_make`.`title`
How would I do the following query in Codeigniter ActiveRecord : -
SELECT *,
(SELECT
image_path
FROM
image
WHERE
image_table = 'model'
AND image_table_id = model_id
GROUP BY image_table_id
LIMIT 1) AS ModelImg
FROM
(SELECT
*
FROM
vw_newcars
where offer_table = 'derivative'
order by offer_order
) x
WHERE make_name = 'Fiat'
group by offer_table_id
limit 12
The part I'm having problems with is how to do a select from subquery in Active Record.
I don't see a from_select function or anything comparable in the documentation.
I managed to get the query to work by putting the from sub_query into the initial select statement :
$this->db->select("*,
(select image_path from image where image_table = 'model' and image_table_id = model_id
group by image_table_id limit 1) as ModelImg FROM
(SELECT * FROM $view where offer_table = 'derivative' order by offer_order) x");
I have a union type query. How can I count the number of rows it queries?
mysql_query(
" (SELECT 1 as sort_col,performerid,pic0
FROM $table
WHERE performerid IS NOT NULL $performeridSql)
UNION
(SELECT 2 as sort_col,performerid,pic0
FROM $table
WHERE performerid IS NOT NULL
$categorySql $buildSql
$breastsize $haircolor $age $ethnicity
$willingnessSql)
ORDER BY sort_col");
Simply count the subquery results...
SELECT COUNT(t.*) FROM
( (SELECT 1 AS sort_col,performerid,pic0
FROM $table
WHERE performerid IS NOT NULL
$performeridSql)
UNION
(SELECT 2 AS sort_col,performerid,pic0
FROM $table
WHERE performerid IS NOT NULL
$categorySql $buildSql
$breastsize $haircolor $age $ethnicity
$willingnessSql)
ORDER BY sort_col)
) AS t
If you need the results in addition to the count, the likes of mysqli_result::num_rows or PDOStatement::rowCount will return what you want. Otherwise, run your query as a subquery, then apply the COUNT aggregate function to the result.