0

I want to select information from 6 table I have 2 choice first one with where statement:

SELECT columns 
FROM   db1, 
       db2, 
       db3, 
       db4, 
       db5, 
       db6 
WHERE  db1.id = $user_id 
        OR db2.user_id = $user_id 
        OR db3.user_id = $user_id 
        OR db4.user_id = $user_id 
        OR db5.user_id = $user_id 
        OR db6.user_id = $user_id 

$user_id is php 1variable` that contain user_id;

Second Choice joins tables and one where statement: like here which one is fast, useful and clear?I want to get fast result because I will do that kind of queries with very complicated queries also

Community
  • 1
  • 1
John
  • 1
  • 2
  • 3
    Cross joining six different table looks like bomb to me – Pரதீப் Jul 07 '16 at 03:50
  • I believe you are actually doing a `CROSS JOIN` here, at least for some of the tables. Is this what you intend? – Tim Biegeleisen Jul 07 '16 at 03:50
  • Its not clear what you want. Can you provide sample data and expected result set ? And as @Prdp mentioned cross joining the six tables is not good idea. So you might want to think about your approach here. – Mahesh Jul 07 '16 at 03:57
  • I am just want to get specific user all information from different tables because I will store them on sessions to not send request every time and get data always from sessions @CoderofCode. – John Jul 07 '16 at 04:10
  • @Prdp I am trying to design like: Seperate all information because I want to achive very fast sql for high traffic problem,For example name table(which contain firstname,lastname user_id of user) or birthday table(which contain year day month user_id) and others: I want to seperate everyhing because in some cases I need only get from database names or birthdays or whatever specific.Is not design for fast table?Because I thought that joining tables is nothing for sql?isn't it?Is it heavy get data from different tables with union? – John Jul 07 '16 at 04:11
  • @TimBiegeleisen I am trying to design like: Seperate all information because I want to achive very fast sql for high traffic problem,For example name table(which contain firstname,lastname user_id of user) or birthday table(which contain year day month user_id) and others: I want to seperate everyhing because in some cases I need only get from database names or birthdays or whatever specific.Is not design for fast table?Because I thought that joining tables is nothing for sql?isn't it?Is it heavy get data from different tables with cross joins? – John Jul 07 '16 at 04:12
  • @John Then you need `UNION` or `UNION ALL` with respect to your need – Mahesh Jul 07 '16 at 04:13
  • @CoderofCode actually it is not different in my case because I have not duplicated rows – John Jul 07 '16 at 04:15
  • What is wrong with my design?@yourcommonsense – John Jul 07 '16 at 05:00
  • Having many 1:1 relations doesn't really make sense. Unless you tested and verified that this overly complicated model does indeed improve performance, I would start with a properly normalized model, i.e. all attributes that belong to a single entity should go into a single table. If you stick to the golden rule that `select *` should be forbidden in production code, I'm pretty sure you won't see any performance problems. Maybe you do when your table contains 500 million rows or more, but then you can still re-factor. –  Jul 07 '16 at 06:52

1 Answers1

1

You want to do this

SELECT columns 
FROM   db1
WHERE id = $user_id
UNION
SELECT columns 
FROM   db2
WHERE id = $user_id
UNION
SELECT columns 
FROM   db3
WHERE id = $user_id
UNION
SELECT columns 
FROM   db4
WHERE id = $user_id
UNION
SELECT columns 
FROM   db5
WHERE id = $user_id
UNION
SELECT columns 
FROM   db6
WHERE id = $user_id

Put an index on the id column on all 6 tables. This will have a performance of O(N) or faster.

You could also user UNION ALL instead of UNION if you want to get the same results from multiple tables (eg leave duplicates). UNION ALL is much faster so use that if you can.

The query you have will cross join all the tables and have a performance of O(N^6) as I'm sure you found out.

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • is UNION fast than OR in this case?(I have promary key for all idis)? I am trying to design like: Seperate all information because I want to achive very fast sql for high traffic problem,For example name table(which contain firstname,lastname user_id of user) or birthday table(which contain year day month user_id) and others: I want to seperate everyhing because in some cases I need only get from database names or birthdays or whatever specific.Is not design for fast table?Because I thought that joining tables is nothing for sql?isn't it?Is it heavy get data from different tables with union? – John Jul 07 '16 at 04:09
  • @john -- Doing it your way will be slow. That is why I posted this answer. Feel free to ask me again -- I'll still give the same answer. You are not doing a join -- a join would involve a `ON x = y` statement and the `JOIN` keyword. You are doing what is called a cross join -- that is every combination of every row in every table with every other one. The result set would have (# rows in db1) x (# rows in db2) x ... x (# rows in db6) – Hogan Jul 07 '16 at 04:21
  • Is my sql design slow?I am just designed it because I have a lot of information and a lot tables.Actually first time My design is like one table 100 column(llike bomb :) ) but it was just test and a lot of people advice that it is not good design thats why I seperate information which I will need seperately.But some cases I should connect them with UNION? How should I design that kind information in the sql? – John Jul 07 '16 at 04:23
  • It sounds like you have a standard denormalized design. In such a design there would be a user table that had one row for each user -- you would not need to look in all the tables. If that is not the case then you don't have a good design – Hogan Jul 07 '16 at 04:25
  • I am just designed it because I have a lot of information and a lot tables.Actually first time My design is like one table 100 column(llike bomb :) ) but it was just test and a lot of people advice that it is not good design thats why I seperate information which I will need seperately.I seperate because a lot of cases I need only choose names,or profileimgs seperately But some cases I should connect them with UNION – John Jul 07 '16 at 04:37
  • Union is fast in this case,but what about join like in link which I shared on my question?What is wrong with my design?I can create user table and get informations from it but problem is user has a lot of information (profileimg,info,name,birthday,country,gender...) and in most cases I need only user name and profileimg only if I create one user table are not there extra information on each sql command to get some user_data? – John Jul 07 '16 at 04:53
  • Perhaps do UNION ALL instead? – jarlh Jul 07 '16 at 06:48
  • @jarlh yes I can use UNION ALL with no problem but problem is database design .I seperate tables with one rule that SQL shouldnt search for data.Ii mean that if I want to get only names I SQL only will return names from name table and will not read other things like mail,info and other kind of information.But this structure problem(but I am not sure yet it is problem,I am trying clarify it for myself) is that when I want to get multiple tables' datas I should run some query that join a lot of table but I am not sure :is it slow or no the difference from select only from one table? – John Jul 07 '16 at 07:28
  • Your answer is not true for my case because columns just reference differect collumns and UNION only can combine same columns – John Jul 07 '16 at 10:48
  • @John -- there is no way for me know that. This was not clear from your original question. Clearly I can't answer you question unless you actually specify what you have. What are the columns of each table. Why do you call your tables db1, db2 etc. They are dbs they are tables. Please specify all the details with example data and expected output and then I can give you a real anwer. – Hogan Jul 07 '16 at 20:45