MySQL Improve performance execution time - mysql

I am trying to improve performance of following query which took 93.2 sec to execute query below:
SELECT year(date), month(date), `country_name_name`,
CEIL(count(res.`user_xmpp_login`) /DAY(LAST_DAY(date))) as avgUser,
CEIL(count(res.user)/DAY(LAST_DAY(date))) as avgPurchase
FROM
( SELECT DATE(`user_registration_timestamp`) as date,
user_country,
NULL as user, `user_xmpp_login`
FROM users
WHERE `user_registration_timestamp` >= "2015-01-01 00:00:00"
AND `user_registration_timestamp` < "2016-01-01 00:00:00"
UNION ALL
SELECT DATE(`ts`) as date, user_country, user, NULL as `user_xmpp_login`
FROM purchase_log p
INNER JOIN users u ON u.`user_xmpp_login` = p.`user`
WHERE `ts` >= "2015-01-01 00:00:00"
AND `ts` < "2016-01-01 00:00:00"
AND result in ('ok', 'cancelled', 'pending')
) AS res
INNER JOIN countries c ON c.`country_id` = res.`user_country`
INNER JOIN country_names cn
ON (cn.`country_name_country` = c.`country_id`
AND cn.`country_name_language` = 'en')
GROUP BY 1,2,3
ORDER BY 4 DESC,5 DESC, 3 ASC;
Explain command shows:
And structure of each table is:
purchase table:
CREATE TABLE `purchase` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`result` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `iuser` (`user`),
) ENGINE=InnoDB AUTO_INCREMENT=12710221 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
users table:
CREATE TABLE `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_country` int(11) DEFAULT NULL,
`user_xmpp_login` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_registration_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_xmpp_login_UNIQUE` (`user_xmpp_login`),
KEY `user_country_FK` (`user_country`),
KEY `user_registration_timestamp` (`user_registration_timestamp`),
CONSTRAINT `users_country_FK` FOREIGN KEY (`user_country`)
REFERENCES `countries` (`country_id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=33504745 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
countries table
CREATE TABLE `countries` (
`country_id` int(11) NOT NULL AUTO_INCREMENT,
`country_code` varchar(2) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`country_id`),
) ENGINE=InnoDB AUTO_INCREMENT=508 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
country names
CREATE TABLE `country_names` (
`country_name_id` int(11) NOT NULL AUTO_INCREMENT,
`country_name_country` int(11) NOT NULL,
`country_name_language` char(2) COLLATE utf8_unicode_ci NOT NULL,
`country_name_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`country_name_id`),
UNIQUE KEY `country_name_country_language_UNIQUE`
(`country_name_country`,`country_name_language`),
KEY `country_name_language` (`country_name_language`),
CONSTRAINT `country_name_country` FOREIGN KEY (`country_name_country`)
REFERENCES `countries` (`country_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=45793 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Is there any recommendations?

If you time each subquery, I think you will find users is the slowest component.
The purchase_log subquery can probably be improved with this "covering" INDEX(result, ts, user).
Combine the two "country" tables!. Use CHAR(2) CHARACTER SET ascii for the PRIMARY KEY and the JOINs to other tables. It is only 2 bytes, unlike INT, which is 4 bytes and VARCHAR..., which is 3 bytes (in this case).
You mention ts, but I don't see where it is coming from. If it is in purchase_log, then that table needs INDEX(user, ts).
What percentage of the users involved 2015? If it is more than about 20%, the INDEX(user_registration_timestamp) won't help.
Consider: Get rid of PRIMARY KEY (country_name_id), and promote the UNIQUE key to PRIMARY.

The biggest problem seems to be in your users table. Remember, mysql can only use one index per table for most situations. On your users table, the user_xmpp_login_UNIQUE column has been used to join it to the purchase_log table. There fore, the user_registration_timestamp index is not being used on the comparison involving the timestamp column.
One suggestion is to create a composite index on the user_xmpp_login and user_registration_timestamp columns.

Related

Can't specify target table t4 for update in FROM clause

I am trying to update my table:
UPDATE offers t1
SET t1.deleted_at = NOW()
WHERE t1.id
NOT IN
(
SELECT f.id
FROM (
SELECT ean, MIN(net_price) as minprice
FROM offers group BY ean
)
as x inner join offers as f on f.ean = x.ean and f.net_price = x.minprice
);
can anybody help me with this issue?
I realized that I needed to solve the issue through left join, but did not understand how to apply it in my case. Thank you.
Here is create table:
CREATE TABLE `offers` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`net_price` decimal(10,2) unsigned DEFAULT NULL,
`retail_price` decimal(10,2) unsigned DEFAULT NULL,
`supplier_id` bigint unsigned DEFAULT NULL,
`manufacturer_id` bigint unsigned DEFAULT NULL,
`stock` int DEFAULT NULL,
`ean` varchar(14) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`mnn_id` bigint unsigned DEFAULT NULL,
`source_id` bigint unsigned NOT NULL,
`nds` smallint unsigned DEFAULT NULL,
`to_export` tinyint(1) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `offers_supplier_id_foreign` (`supplier_id`),
KEY `offers_manufacturer_id_foreign` (`manufacturer_id`),
KEY `offers_mnn_id_foreign` (`mnn_id`),
KEY `offers_source_id_foreign` (`source_id`),
CONSTRAINT `offers_manufacturer_id_foreign` FOREIGN KEY (`manufacturer_id`) REFERENCES `manufacturers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `offers_mnn_id_foreign` FOREIGN KEY (`mnn_id`) REFERENCES `mnns` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `offers_source_id_foreign` FOREIGN KEY (`source_id`) REFERENCES `sources` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `offers_supplier_id_foreign` FOREIGN KEY (`supplier_id`) REFERENCES `suppliers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=25762 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
The error you are getting is
You can't specify target table 't1' for update in FROM clause
This is a problem specific to MySQL. You cannot directly access the updated table in a subquery, you need a sub subquery :-)
Replace
inner join offers as f
by
inner join (select * from offers) as f
hence to get the query working.
You don't need the join by the way. You can just write
UPDATE offers
SET deleted_at = NOW()
WHERE (ean, net_price) NOT IN
(
SELECT ean, MIN(net_price)
FROM (select * from offers) o
GROUP BY ean
);
Another way to write this (update all rows for which exists a lower price for the same EAN):
UPDATE offers t1
SET t1.deleted_at = NOW()
WHERE EXISTS
(
SELECT NULL
FROM (SELECT * FROM offers) t2
WHERE t2.ean = t1.ean
AND t2.net_price < t1.net_price
);
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=bfa6d74a72d40fa445ea5fa9f95803d1
By the way: You may want to add the condition deleted_at IS NULL to your update statement in order not to update rows that are already logically deleted, if such rows exists.

How to order by calculated column in mysql

It is very slow to order by calculated column. Like this one:
select
`users`.`id`,
`username`,
`about_me`,
(
select
count(*)
from
`interests`
inner join `users_interests` on `interests`.`id` = `users_interests`.`interest_id`
where
`users`.`id` = `users_interests`.`user_id`
and `interest_id` in (1, 2, 4) --this is ids of interests which authenticated user has chosen
) as `interests_count`
from
`users`
order by
`interests_count` desc
So in this query i am ordering users By interests which is best suiting authenticated user. And it is very slow in large data tables. Any ideas what will be alternative and much faster solution. Thanks in advance
EDIT
Users Table
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`roles_id` bigint(20) unsigned NOT NULL DEFAULT 2,
`username` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`about_me` varchar(70) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`email` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`status` tinyint(1) NOT NULL DEFAULT 0,
`email_verified_at` timestamp NULL DEFAULT NULL,
`password` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_username_unique` (`username`),
UNIQUE KEY `users_email_unique` (`email`),
KEY `users_roles_id_foreign` (`roles_id`),
CONSTRAINT `users_roles_id_foreign` FOREIGN KEY (`roles_id`) REFERENCES
`user_roles` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=50102 DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci
Interests Table
CREATE TABLE `interests` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`interest_category_id` bigint(20) unsigned NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `interests_interest_category_id_foreign` (`interest_category_id`),
CONSTRAINT `interests_interest_category_id_foreign` FOREIGN KEY
(`interest_category_id`) REFERENCES `interests_categories` (`id`) ON DELETE
CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=61 DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci
Pivot table between Users and Interests
CREATE TABLE `users_interests` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) unsigned NOT NULL,
`interest_id` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `users_interests_user_id_foreign` (`user_id`),
KEY `users_interests_interest_id_foreign` (`interest_id`),
CONSTRAINT `users_interests_interest_id_foreign` FOREIGN KEY (`interest_id`) REFERENCES `interests` (`id`) ON DELETE CASCADE,
CONSTRAINT `users_interests_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=251552 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
Without test data it's difficult to know if this would be faster than your current query, but you might find the use of a Common Table Expression (a.k.a. WITH clause) to be helpful here:
with cteCount AS (select ui.`user_id`,
count(*) as `interests_count`
from `interests` i
inner join `users_interests` ui
on i.`id` = ui.`interest_id`
where ui.`interest_id` in (1, 2, 4))
select u.`id`,
`username`,
`about_me`,
cc.`interests_count`
from `users` u
inner join cteCount cc
on cc.`user_id` = u.`id`
order by
cc.`interests_count` desc
The performance of this query will be affected by the presence or absence of indexes on the appropriate columns. Just glancing at it I'd say you should have the following indexes available:
interests
Index 1: id
users_interests
Index 1: interest_id
users
Index 1: id
Try to directly join and aggregate.
SELECT u.id,
u.username,
u.about_me,
count(ui.interest_id) interests_count
FROM users u
LEFT JOIN users_interests ui
ON ui.user_id = u.id
AND ui.interest_id IN (1, 2, 4)
GROUP BY u.id,
u.username,
u.about_me;
Additionally create an index supporting the aggregation.
CREATE INDEX users_id_username_about_me
ON users
(id,
username,
about_me);

Understanding MySql queries

Trying to teach my self a bit of mysql and php and decided to do that by working on actual project with help oh "how to do everything with MySQL and PHP book".
first problem i have is with understanding joint table queries.
here are my tables:
CREATE TABLE `clients` (
`id` SMALLINT(6) NOT NULL AUTO_INCREMENT,
`client` VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `KlientID` (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=8;
CREATE TABLE `facilities` (
`id` SMALLINT(6) NOT NULL AUTO_INCREMENT,
`facility` VARCHAR(45) NOT NULL,
`fk_client` SMALLINT(6) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `fk_idklijent_idx` (`fk_client`),
CONSTRAINT `FK_client_id` FOREIGN KEY (`fk_client`) REFERENCES `clients` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=35;
CREATE TABLE `models` (
`id` SMALLINT(6) NOT NULL AUTO_INCREMENT,
`model` VARCHAR(50) NOT NULL,
`fk_manufacturer` SMALLINT(6) NOT NULL,
PRIMARY KEY (`id`),
INDEX `ModelID` (`id`),
INDEX `fk_proizvodjacID_idx` (`fk_manufacturer`),
CONSTRAINT `FK_manuf_id` FOREIGN KEY (`fk_manufacturer`) REFERENCES `manufacturers` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=9;
CREATE TABLE `machines` (
`id` SMALLINT(6) NOT NULL AUTO_INCREMENT,
`serial` VARCHAR(50) NOT NULL,
`fk_model` SMALLINT(6) NOT NULL,
`InvBr` INT(11) NULL DEFAULT '0',
`fk_facilities` SMALLINT(6) NULL DEFAULT '0',
`sw` VARCHAR(255) NULL DEFAULT NULL,
`adaptation` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `FK_uredjaji_modeli` (`fk_model`),
INDEX `FK_uredjaji_poslovnice` (`fk_facilities`),
INDEX `Index 4` (`serial`),
CONSTRAINT `FK_facility_id` FOREIGN KEY (`fk_facilities`) REFERENCES `facilities` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT `FK_models_id` FOREIGN KEY (`fk_model`) REFERENCES `models` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=93;
CREATE TABLE `technicians` (
`id` SMALLINT(6) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `ServiserID` (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=5;
CREATE TABLE `workorders` (
`id` SMALLINT(6) NOT NULL AUTO_INCREMENT,
`wo_nr` VARCHAR(50) NOT NULL,
`fk_machine_id` SMALLINT(6) NOT NULL,
`fk_technitian_id` SMALLINT(6) NOT NULL,
`counter` INT(11) NOT NULL DEFAULT '0',
`service_date` DATE NOT NULL,
`description` LONGTEXT NOT NULL,
`work_hours` INT(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
INDEX `FK_rn_serviseri` (`fk_technitian_id`),
INDEX `FK_machines_id_idx` (`fk_machine_id`),
CONSTRAINT `FK_machines_id` FOREIGN KEY (`fk_machine_id`) REFERENCES `machines` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT `FK_technitian_id` FOREIGN KEY (`fk_technitian_id`) REFERENCES `technicians` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=1393;
using this query i get each row 8 times.
SELECT DATE_FORMAT (w.service_date, '%d.%m.%Y'), f.facility, m.model, mc.serial, w.description, t.name
FROM workorders AS w, facilities AS f, models AS m, machines AS mc, technicians AS t
WHERE f.id = mc.fk_facilities AND w.fk_machine_id = mc.id AND w.fk_technitian_id = t.id AND w.service_date > '2009-12-31'
ORDER BY w.service_date DESC;
can someone pls point me to what i'm doing wrong. i only need rows from workorders table. other tables are here only to show real data not only id's - bad, not true :(
thx
edit 1:
i need a list of workorders. to explain my question little more here is how the result should look like:
[date][name of the facility the machine (fk_machine_id) belongs to][model of the machine (fk_machine_id)][serial number of the machine (fk_machine_id)][description from workorder][technician name (fk_technician_id)]
edit2:
here is the model image
i think my problem is in fact that to get the model i need to check with machines table first. Same thing with facility.
got it!
the problem was in one missing AND.
final SELECT looks like this:
SELECT DATE_FORMAT (w.service_date, '%d.%m.%Y') AS service_date, w.wo_nr, f.facility, m.model, mc.serial, FORMAT (w.counter, 0) AS Counter, w.description, t.name AS technician
FROM workorders AS w, technicians AS t, machines AS mc, models AS m, facilities AS f
WHERE mc.fk_facilities = f.id
AND w.fk_machine_id = mc.id
AND mc.fk_model = m.id
AND w.fk_technitian_id = t.id
AND w.service_date > '2009-12-31'
ORDER BY w.service_date DESC;
thx all. moving on to the next problem :)
to get only rows from workorders table use this query
SELECT w.*
FROM workorders AS w, facilities AS f, models AS m, machines AS mc, technicians AS t
WHERE f.id = mc.fk_facilities AND w.fk_machine_id = mc.id AND w.fk_technitian_id = t.id AND w.service_date > '2009-12-31'
ORDER BY w.service_date DESC;
You say you only want data from the workorders table. Then you don't need all the other joins.
SELECT * FROM workorders WHERE service_date > '2009-12-31'
Unless you need the columns in their tables as well.
Not too sure exactly what you require though.
Only put the columns you want returned in your results set in the SELECT section of your query. You can still include columns from other tables in your joins, they just won't be displayed. Also if you are getting multiple rows then either your data contains duplicates in which case you should look at your keys and constraints or you are missing one or more joins.
Would this work for you?
SELECT DISTINCT DATE_FORMAT (w.service_date, '%d.%m.%Y') as service_date,
f.facility, m.model, mc.serial, w.description, t.name
FROM workorders AS w, facilities AS f, models AS m, machines AS mc, technicians AS t
WHERE f.id = mc.fk_facilities
AND w.fk_machine_id = mc.id
AND w.fk_technitian_id = t.id
AND w.service_date > '2009-12-31'
ORDER BY w.service_date DESC;

Create SQL query that gets data from several tables WHEN ID is in one table

I am trying to create a news feed and from previous questions have found the simplest way to execute it but do not know what kind of query I should be making in SQL.
I have a table titled artists that has the members ID, artist name and url to their page, all need to be retrieved. If an ID is not in this table it should not be included in the output of the statement.
I have several tables with similar structures such as tracks, status and news. There are more but we'll stick to those three for this question and I can add more late on. In these tables is the ID among other things and I shall provide these later.
The third table is one titled events. Data is inserted in here via a trigger AFTER data is added to any of the other tables I have referenced. This table contains the ID, the action (so something like 'has uploaded a track.') and the timestamp.
ARTIST TABLE;
CREATE TABLE `artists` (
`ID` int(11) NOT NULL,
`name` varchar(100) COLLATE latin1_general_ci NOT NULL,
`url` varchar(100) COLLATE latin1_general_ci NOT NULL,
`DP` varchar(200) COLLATE latin1_general_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `url` (`url`),
UNIQUE KEY `DP` (`DP`),
KEY `ID` (`ID`),
CONSTRAINT `artists_ibfk_1` FOREIGN KEY (`ID`) REFERENCES `members` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
TRACKS
CREATE TABLE `tracks` (
`ID` int(11) NOT NULL,
`url` varchar(200) COLLATE latin1_general_ci NOT NULL,
`name` varchar(100) COLLATE latin1_general_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`track_ID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`track_ID`),
UNIQUE KEY `url` (`url`),
UNIQUE KEY `track_ID` (`track_ID`),
KEY `ID` (`ID`),
CONSTRAINT `tracks_ibfk_1` FOREIGN KEY (`ID`) REFERENCES `members` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=111112 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
NEWS
CREATE TABLE `news` (
`ID` int(11) NOT NULL,
`article` text COLLATE latin1_general_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
KEY `ID` (`ID`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
STATUS
CREATE TABLE `status` (
`ID` int(11) NOT NULL,
`status` varchar(300) COLLATE latin1_general_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
KEY `ID` (`ID`),
CONSTRAINT `status_ibfk_1` FOREIGN KEY (`ID`) REFERENCES `artists` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
EVENTS
CREATE TABLE `events` (
`ID` int(11) NOT NULL,
`action` varchar(100) COLLATE latin1_general_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
KEY `ID` (`ID`),
CONSTRAINT `events_ibfk_1` FOREIGN KEY (`ID`) REFERENCES `members` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='This table shows what the artist has done and is used feed'
The query I have tried and it failed. This one only references the tracks and events tables. I am not sure how to add the other tables to the query.
SELECT
T.url,
T.ID,
E.action,
E.ID,
E.timestamp
FROM tracks T
JOIN events E ON T.ID = E.ID
WHERE T.ID = E.ID AND E.action = 'has uploaded a track.'
ORDER BY E.timestamp DESC
To answer extremely valid comment, the query is in an AJAX requested page for a news feed. The output will be something along the lines of;
An artist has uploaded a track.
Track goes here
Formatted timestamp here
Another artist has some news.
News goes here
Formatted timestamp here
Hopefully that will give you an idea.
All data by the end of it should be ordered by timestamp. Thanks in advance.
If you have a single table with all events in it, and the message that you want to show, I'm confused why you would have to join in any of the other tables---maybe other than your member table, which would give you the user's name. Since you want each event in its own record, there's no need for any kind of aggregation. (Although, if you did want to display a comma-separated list of actions for a user, have a look at GROUP_CONCAT.)
For example a query like:
SELECT
CASE
WHEN events.table_name = "tracks" THEN "has uploaded a track."
WHEN events.table_name = "news" THEN "has some news."
END AS description
, events.action
, DATE_FORMAT(events.when_it_happened, "%a %l%p") AS when_it_happened
, members.username
, members.ID
FROM events
INNER JOIN members ON (events.ID = members.ID)
WHERE events.when_it_happened >= NOW() - INTERVAL 5 DAY
ORDER BY events.when_it_happened DESC
Would give you all events that all users performed within the last 5 days; the most recent first.

Nested "select ... in" performance is slow - how to fix?

Here I have a simple join query. If first two queries get results, the whole query can be done in 0.3 secs, but if the first 2 select doesn't fetch any result, the whole query will cost more than half a minute. What causes this difference? How to fix this problem and improve the performance?
SELECT * FROM music WHERE id IN
(
SELECT id FROM music_tag_map WHERE tag_id IN
(
SELECT id FROM tag WHERE content ='xxx'
)
)
LIMIT 10
Here's the table structure:
CREATE TABLE `tag` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `index2` (`content`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `music` (
`id` int(7) NOT NULL AUTO_INCREMENT,
`name` varchar(500) NOT NULL,
`othername` varchar(200) DEFAULT NULL,
`player` varchar(3000) DEFAULT NULL,
`genre` varchar(100) DEFAULT NULL,
`sounds` text,
`create_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `player` (`player`(255)),
KEY `name` (`othername`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `music_tag_map` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`music_id` int(7) NOT NULL,
`tag_id` int(7) NOT NULL,
`times` int(11) DEFAULT '1',
PRIMARY KEY (`id`),
KEY `music_id` (`music_id`),
KEY `tag_id` (`tag_id`),
CONSTRAINT `music_tag_map_ibfk_1` FOREIGN KEY (`id`) REFERENCES `music` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `music_tag_map_ibfk_2` FOREIGN KEY (`tag_id`) REFERENCES `tag` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
There are no joins in that query; there are two sub-selects.
A joined query would be:
SELECT *
FROM music
JOIN music_tag_map ON music.id=music_tag_map.id
JOIN tag ON music_tag_map.tag_id=tag.id
WHERE tag.content = ?
LIMIT 10;
An EXPLAIN applied to each will show you why the join performs better than the sub-select: the sub-select will scan the entire music table (the primary query), while the optimizer can pick the order of tables to scan for the joins, allowing MySQL to use indices to get only the needed rows from all the tables.