mysql optimization - display 10 most recent records, but also identify duplicate rows -
I'm new to mysql and I'm pulling my hair about this problem for days. I need to improve / adapt this question so that it runs fast - just taking 5 secs
Here is the query:
SELECT SQL_NO_CACHE COUNT (*) Multiple, as a *, B. * On announcements as an INNER Join Store. Username = s.username WHERE s.username is not empty and s.state = 'NC' group by a.announcement_id ORDER by a.dt DESC LIMIT 0,10
Store table contains the following: announce_id, msg, dt, usernames: store_id, username, name, state, city, zip, etc ...
announcements
There are about 10,000 records in the reserves table and there are about 500,000 records in the manifesto.
I am trying to complete in English - displays the 10 most recent store announcements, but what makes it complicated is that the same table in the table of the same user ID (one row per location) Together there may be several entries in the store table. So if a chain store, "Chiptal" sends an announcement, then I want to display only one line with a note next to their declaration, which says that "there are many places in this store" that is why I am using the count (*) and the group, so if the count (*) & gt; 1
I know there are many places for declaration.
Using SQL_NO_CACHE where status can be any state, city or zip because announcements are updated frequently, so you rarely get the same result, does that mean?
I really appreciate this about how to do it better, I know a bit about indexes, but I have created an index for the "username" field in both tables. I feel free to separate from here, I know that I have to remember something.
Update -
DESC Store;
field type tap key default additional store_id int (11) no PRI tap auto_increment username name varchar (20) no multi null name varchar (100) no tap road varchar (100) no tap city varchar (50) No faucet state varchar (2) no faucet zarch varchar (15) no faucet
DESC announcements;
field type tap key default additional dt datetime no tap user name varchar (20) no multilingual message varchar (200) none announcement declaration_id int (11) no PRI tap auto_increment
EXPLAIN output;
id select_type table type possible_key key's key Y_len referee strokes additional 1 simple using an index user name primary 47 faucet 315001; Using the fileort 1 SIMPLE b Ref user name Username 62 a.username 1
Try something like this:
SELECT SQL_NO_CACHE COUNT (*) as multiple, a. *, B. * In the form of declarations in the form of INNER JOIN (SELECT username, COUNT (username) as in the form of as much as the username where the username is not, and the state = 'NC' 'by the group) Name = s.username ORDER by a.dt DESC limit 10
Comments
Post a Comment