I have a feed table with say fields:
id– unique feed idcreated– the date the feed was createdtable– the name of the table the rest of the feed info resides
Then I have say 2 tables: feed_image and feed_text. Now these 2 tables contain different information about a feed, different fields.
How is it possible (in MySQL) to extract the information for the feed from the appropriate table which name is specified in feed.table?
Here is how my schema looks like:
+------------------+
| table_a |
+---------------------+ |------------------|
| feed | | id |
|---------------------| +------+ feed_id |
| id <-------------------+-+ | field_in_a |
| created | | | ... |
| table | | | |
| | | | |
| | | | |
| | | +------------------+
+---------------------+ |
|
|
| +-------------------+
| | table_b |
| |-------------------|
| | id |
+--------+ feed_id |
| field_in_b |
| ... |
| |
| |
| |
| |
+-------------------+
Each feed exists either in table_a or table_b or table_c or … (I have like 30 of them).
How can I specify which table to extract the info from (each table has a different structure).
Or, if I add indexes on each table_*.feed_id and map it to feed.id, would InnoDB do some magic, so when I JOIN them all it would look in just one of them, not all 30?
My latest idea is to have just one table feed with a field feed.content where I would store a serialized PHP object of a different PHP class representing the different feed type and its individual contents.
What is the best way to go regarding performance?
P.S.: No records would need to be selected / searched / ordered by individual parameters, just by created. The idea should be able to work well with 1 000 000+ records.
UPDATE:
To clarify about the 30+ table_a/b/c..
Each feed can be of too many different types (new ones will also be added with time):
- An image feed would have VARCHAR(255)
urlfield - A text feed would have LONGTEXT
textfield - A youtube.com feed would have VARCHAR(255)
title, VARCHAR(255)video_idfields - A *.com feed would have *
x1, *x2, *x3… fields
Each of these feeds will be then displayed with PHP according to type:
- An image will be displayed as na image from the given URL
- A text will be displayed as a pure text
- A youtube.com feed would display a video player with the given title from the given video id
- A *.com feed would display… 🙂
I would use a LEFT JOIN and alias my columns in the select and alias my tables in the join allowing you to return any and all information you need.
The with whatever language your pulling the results you can group and perform logic as necessary.
UPDATE:
Why do you have 30 tables exactly? Maybe one “meta” table with the feed creation date url it came from etc… and another table that contains a unique record id, feed id, content, content type.
That way you can join on one table where feed id’s match as well as group by or filter by content type.
Visualization: Feed table
Visualization: Feed Resources table