How to make this MySQL query execute faster? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
SELECT * FROM (`collection_series`)
JOIN `datadatexnumericy` ON
`collection_series`.`series_id` = `datadatexnumericy`.`series_id`
JOIN `saved_users_chart` ON `collection_series`.`collection_id` =
`saved_users_chart`.`chart_id`
WHERE `saved_users_chart`.`chart_id` = '265'
AND `saved_users_chart`.`id` = '6' AND `datadatexnumericy`.`x` >=
'1973-09-30' AND `datadatexnumericy`.`x` <= '2014-06-30' AND
`datadatexnumericy`.`series_id` != '43538'
AND `datadatexnumericy`.`series_id` != '43541'
GROUP BY YEAR(datadatexnumericy.x)
This is my SQL query and result of this query I am getting from AJAX response this query is working fine but I am getting response slow a bit I think the problem is in my SQL query.
I want all matching records from collection_series and datadatexnumericy table and only matching row from saved_users_chart is there any possible way to optimize this query in more efficient way so that I can get ajax response faster.

In my opinion your query is already optimized. You should test the result of the query in an SQL server to see response time of SQL server and then compare with the time you actualy receive the response back from server.
If is not necesary to extract all the columns from each table, then manualy enter the column names instead of *. By doing this the response received from server will be certainly smaller, therefore it will be faster.

Related

sort a JSON array with PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to register for youtube API statistics as JSON data to its proper database. I figured out listing whole data so far,
but I'm not able to list and sort the individual data, which stands for "dislikeCount", descending from more to less.
The JSON data I mentioned in "$ Row ['page_content']" section is below. and I want the "dislikeCount" data to be sorted from large to small.
Any help will be appreciated.
Database page_content
{
"kind":"youtube#video",
"etag":"LVLEiwEdUU043rY6JqiUrm7FmYY",
"id":"IMiBrszc40s",
"statistics":{
"viewCount":"6416534",
"likeCount":"26553",
"dislikeCount":"1862",
"favoriteCount":"0",
"commentCount":"987"
}
}
index.php
$query = $db->from('pages')->orderBy('page_id', 'ASC')->all();
if ($query) {
foreach ($query as $row) {
$json = json_decode($row['page_content'], true);
$videoView = $json['statistics']['viewCount'];
$videoLike = $json['statistics']['likeCount'];
$videoComment = $json['statistics']['commentCount'];
echo $videoView;
echo $videoLike;
echo $videoComment;
}
}
From what I understand from your question, you have a database table pages. This table has a JSON column, page_content, which contains the JSON as you've specified. Now you want to sort the pages based on the pages.page_content.statistics.dislikeCount?
If you're using MySQL or PostgreSQL, then you can order the resultset in the SQL query directly. For example, for MySQL (untested, as I don't have mysql running locally right now):
SELECT * FROM pages
ORDER BY page_id, page_content->"$.statistics.dislikeCount"
If SQL is not an option, you can use PHP's usort.

What is different binding and duration of mysql query? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have log from laravel that showing the binding time and duration time of query execution:
select * from `yg_product_detail` where `product_id` = '5551973459553' and `yg_product_detail`.`deleted_at` is null limit 1
Metadata
Bindings 0. 55197345953
Hints Use SELECT * only if you need all columns from table
LIMIT without ORDER BY causes non-deterministic results, depending on the query exection plan
Duration 26.8s
And as seen above, my duration time is too long, but I need to make sure, is the duration time the real time from mysql query? or process logic between another query?
because when I try to run those query to mysql, its run fast.
Thank you
Bindings are values inserted into your SQL query. The execution time displayed on the upper right corner is the actual round-trip time the SQL query took to execute (i.e. the time elapsed between sending the query to the DBMS and the DBMS returning the result as well as parsing the result into models).
If you have an Eloquent query like this:
User::where('name', 'like', '%john%')
->orderBy('age')
->get();
Then it will be converted to a prepared SQL statement like this:
select *
from `users`
where `name` like ?
order by `age`
where the ? gets replaced by a binding, in this case '%john%'. In your case, the binding is the product_id which you attempted to cross out in the query but left in the query explanation table.

Combining database query request in a single statement [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have been told that my web server only allows 75,000 database queries request per hour. I'm wondering if combining query request would lessen the amount of queries or if it doesn't matter. It kind of feel like common sense, but I just want to make sure. Thanks.
$text1 = "text1";
$text2 = "text2";
$text3 = "text3";
SELECT text FROM table WHERE text = '$text1';
SELECT text FROM table WHERE text = '$text2';
SELECT text FROM table WHERE text = '$text3';
3 queries ^---
SELECT text FROM table WHERE
text = '$text1' OR
text = '$text2' OR
text = '$text3';
1 query ^---
Is the above correct or wrong? Thanks.
It look OK. You also can use "IN", the you can write i little bit smaler.
SELECT text FROM table WHERE
text IN ('$text1', '$text2', '$text3');

Left join vs two queries, which is faster? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Supposing I have two tables to query, which query should I choose?
Left join between them or two separate queries? Which option is faster?
Same question on three tables (so basically double left join vs. three queries), which is faster?
i think only one query is better. But you should try in your SQL Server both queries and see which take a longer time
Join query is intoduced for faster data fetching..It consumes execution time..So no doubt join query is much faster...When you use seperate query then each time it has to go database and fetch result but when join all queries,it needs only single time travel to db...

Insert each result of an array into separate SQL rows? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a query that is returning a different amount of user_id's each time it's run (based on the number of subscribers).
What I need to do is insert each of these user_id results into separate rows within a table along with a simple message of "new alert" in a separate column.
How could I possibly go about doing this? Would a for each loop work in this situation?
Try this:
INSERT INTO alert_table SELECT user_id, 'new alert' FROM ... WHERE ...
Use your own query, just prepend it with the INSERT INTO alert_table clause.

Categories