How can i make order by faster? - mysql

When i use ORDER BY in large table (160000 rows) like this:
select HomeTeam,AwayTeam,FTR,FTHG,FTAG,HS,`AS`,`Date`,Elo1A,Elo1B,Elo2A,Elo2B,Elo3A,Elo3B,Elo4A,Elo4B,nElo1A,nElo1B,nElo2A,
nElo2B,nElo3A,nElo3B,nElo4A,nElo4B from historic
where (HomeTeam='Crystal Palace' or AwayTeam='Crystal Palace') and datediff('1994-08-20',Date)>0 order by `Date` desc limit 1
fetch duration is 1.078 sec / 0.000 sec
When i use without ORDER BY like this:
select HomeTeam,AwayTeam,FTR,FTHG,FTAG,HS,`AS`,`Date`,Elo1A,Elo1B,Elo2A,Elo2B,Elo3A,Elo3B,Elo4A,Elo4B,nElo1A,nElo1B,nElo2A,
nElo2B,nElo3A,nElo3B,nElo4A,nElo4B from historic
where (HomeTeam='Crystal Palace' or AwayTeam='Crystal Palace') and datediff('1994-08-20',Date)>0 limit 1
fetch duration is 0.187 sec / 0.000 sec
Problem is that without ORDER BY i get wrong result:
Crystal Palace Tranmere 1993-08-14
but correct is with ORDER BY like this:
Crystal Palace Watford 1994-05-08
How can i avoid ORDER BY or somehow smarter call last match from current match?

As per our conversation in comments you have created index on date field and getting performance by it-
Further you can try below query which may provide you better performance-
SELECT HomeTeam,AwayTeam,FTR,FTHG,FTAG,HS,`AS`,`Date`,Elo1A,Elo1B,Elo2A,Elo2B,Elo3A,Elo3B,Elo4A,Elo4B,nElo1A,nElo1B,nElo2A,
nElo2B,nElo3A,nElo3B,nElo4A,nElo4B FROM historic
WHERE (HomeTeam='Crystal Palace' OR AwayTeam='Crystal Palace') and `Date` < '1994-08-20' ORDER BY `Date` DESC LIMIT 1;
Note: Here I just remove function from date field so that it can use index to filter data also.

Related

Getting last 30 days of records

I have a table called 'Articles' in that table I have 2 columns that will be essential in creating the query I want to create. The first column is the dateStamp column which is a datetime type column. The second column is the Counter column which is an int(255) column. The Counter column technically holds the views for that particular field.
I am trying to create a query that will generate the last 30 days of records. It will then order the records based on most viewed. This query will only pick up 10 records. The current query I have is this:
SELECT *
FROM Articles
WHERE DATEDIFF(day, dateStamp, getdate()) BETWEEN 0 and 30
LIMIT 10
) TOP10
ORDER BY Counter DESC
This query is not displaying any records, but I don't understand what I am doing wrong. Any suggestions?
The MySQL version of the query would look like this:
SELECT a.*
FROM Articles a
WHERE a.dateStamp >= CURDATE() - interval 30 day
ORDER BY a.counter DESC
LIMIT 10;
Your query is generating an error. You should look at that error before fixing the query.
The query would look different in SQL Server.

Simple SQL query taking a long time

I have a query that I use to determine what the interval is between timestamps of gauge data using the 2 most recent readings:
$interval_query = sprintf("SELECT `stamp`
FROM `processed_gauge_data`
WHERE `processed_gauge_data`.`gauge_id` IN (%s)
ORDER BY `processed_gauge_data`.`stamp` DESC LIMIT 2;",
$gauge_id
);
Here is an image with EXPLAIN results as well as the structure of the table:
http://i.imgur.com/QJmHmeb.png?1
This has worked fine for most gauges, but there are 2 in particular that it takes 30-45 seconds to execute this query. Selecting all data for those 2 gauges takes less than a second. What is causing this? I don't understand what's going on.
Turns out it was because of ORDER BY processed_gauge_data.stamp DESC. I changed my query to ORDER BY 'id' and it went from 30-45 seconds to .0006-.0003 seconds:
$interval_query = sprintf("SELECT `stamp`, 'id'
FROM `processed_gauge_data`
WHERE `processed_gauge_data`.`gauge_id` IN (%s)
ORDER BY `processed_gauge_data`.`id` DESC LIMIT 2;",
$gauge_id
);

How ORDER BY works in MySQL?

assume there is a table with 100 million records in it with this schema:
+---------+---------+------+
| user_id | post_id | date |
+---------+---------+------+
We want to have latest 30 records which user with #1 is posted.
SELECT * FROM users_posts WHERE user_id = 1 ORDER BY date DESC LIMIT 30
How MySQL search in table ? Does it search all rows ( full scan ) finding all matches THEN ordering them by date?
if Yes( Full Scan ), how can we edit this query to limit it to search latest records till finds 30 matches. ( to increase reading speed and have better performance )
thanks in advanced.
If you want the best performance for this query:
SELECT *
FROM users_posts
WHERE user_id = 1
ORDER BY date DESC;
Then you want an index on users_posts(user_id, date). This will allow the SQL engine to only use the index to define the set of rows being used. First, it will use user_id to satisfy the where clause. Then it will use the date part for the order by. It will still have to look up the records in the original data pages, in order to get all the columns needed for the select.
If you want to limit this to 30 rows, then add limit 30:
SELECT *
FROM users_posts
WHERE user_id = 1
ORDER BY date DESC
LIMIT 30;
With the above index, this will improve performance. With no index, the engine will have to scan all the data, sort the data that matches the where clause, and then apply the limit -- in other words, it won't be much of an improvement in performance.

Sqlite Query select statement with sorted result respecting the OFFSET

I want to make a sqlite query in such a way that the result should be sorted which has a LIMIT and the OFFSET. But the OFFSET should work in synch a manner that it should discard the last records from the result.
SELECT * FROM TempTable WHERE CLASS = 1 ORDER BY Date ASC LIMIT 100 OFFSET 5;
The above query just ignores the first 5 records from the table and give the remaining records. But instead I want it to ignore the first 5 latest entries.
Note:- the first 5 latest entries means since I am sorting it by date it should IGNORE the latest record inserted in the table respecting the date.
Sort backwards, with OFFSET 5 and resort again:
SELECT * FROM (
SELECT * FROM TempTable WHERE CLASS = 1 ORDER BY Date DESC LIMIT 100 OFFSET 5
) ORDER BY Date ASC;

mysql select with this query then pad out with this query?

I am trying to do a mysql query to select some news stories from a table, now the key is I always need 5 results.
so I was hoping to be able to pad out my results with another where clause ie
select * from here where this = 1
if there is < 5 results then select * from here where this = 2
limit [how ever many we are short say the first query brings back 3 results this should bring back 2]
Now I've looked at using a union to do this but without outside help ie from php and another query to count the results I don't think it is possible, I know I could simply use php to do this, and will probably end up doing that, but I was just wondering if what I am trying to do is possible with one mysql query?
EDIT:
also it needs to order by date but they are not really posted in order so
order by date get upto 5 where this = 1 and if there isn't 5 pad it out with the remainder of where this = 2 also ordered by date.
Another Shameful Edit:
ask a silly question lol... it was my sleep deprivation I just assumed there was data in the table and the previous coder was using unions to do all sorts of stuff, making me think it was more complex than it should be
SELECT *
FROM
news
WHERE
( this = 45 || this= 0 )
AND
active = '1'
ORDER BY
this ASC,
date_added DESC
LIMIT 5
How about -
SELECT *
FROM here
WHERE this < 5 -- added this WHERE clause based on the idea that there will be at least one item per this
ORDER BY this ASC, `date` ASC
LIMIT 5;
Or are you after the five results then being sorted by date again -
SELECT *
FROM (
SELECT *
FROM here
WHERE this < 5 -- added this WHERE clause based on the idea that there will be at least one item per this
ORDER BY this ASC, `date` ASC
LIMIT 5
) AS tmp
ORDER BY `date` ASC
You could combine the where clauses and use limit :
select * FROM here WHERE this = 1 OR this = 2 ORDER BY this LIMIT 5
Even in there were 15 records where this is equal to 1 this would only bring back 5 records ...