Jump to content

Which is better query?

- - - - -

  • Please log in to reply
4 replies to this topic

#1
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
hi guys

i'll using mysql

please select an better query for those tables:

CREATE TABLE IF NOT EXISTS `msgUSR` (

  `user_id` int(11) NOT NULL AUTO_INCREMENT,

  `nick` varchar(255) NOT NULL,

  `email` varchar(255) NOT NULL,

  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

  PRIMARY KEY (`user_id`)

);


CREATE TABLE IF NOT EXISTS `msgING` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `msg` text NOT NULL,

  `user` int(11) NOT NULL,

  `to_user` int(10) unsigned NOT NULL,

  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  PRIMARY KEY (`id`)

);



Query1:
SELECT `msgING`.`msg` , `msgUSR`.`nick` , `msgUSR`.`email`

FROM `msgUSR`

RIGHT JOIN `msgING` ON `msgUSR`.`user_id` = `msgING`.`user`

WHERE `msgUSR`.`user_id` = '3'
[Showing rows 0 - 21 (22 total, Query took 0.0004 sec)]

Query2:
SELECT `msgING`.`msg` , `msgUSR`.`nick` , `msgUSR`.`email`

FROM `msgUSR`

RIGHT OUTER JOIN `msgING` ON `msgUSR`.`user_id` = `msgING`.`user`

WHERE `msgUSR`.`user_id` = '3'
[Showing rows 0 - 21 (22 total, Query took 0.0004 sec)]

Query3:
SELECT `msgING`.`msg` , `msgUSR`.`nick` , `msgUSR`.`email`

FROM `msgUSR`

LEFT JOIN `msgING` ON `msgUSR`.`user_id` = `msgING`.`user`

WHERE `msgUSR`.`user_id` = '3'
[Showing rows 0 - 21 (22 total, Query took 0.0005 sec)]

Query4:
SELECT `msgING`.`msg` , `msgUSR`.`nick` , `msgUSR`.`email`

FROM `msgUSR`

LEFT OUTER JOIN `msgING` ON `msgUSR`.`user_id` = `msgING`.`user`

WHERE `msgUSR`.`user_id` = '3'
[Showing rows 0 - 21 (22 total, Query took 0.0006 sec)]

Query5:
SELECT `msgING`.`msg` , `msgUSR`.`nick` , `msgUSR`.`email`

FROM `msgUSR` , `msgING`

WHERE `msgUSR`.`user_id` = `msgING`.`user`

AND `msgUSR`.`user_id` = '3'
[Showing rows 0 - 21 (22 total, Query took 0.0004 sec)]



thank you so much

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
It depends on what you want returned. 1 and 2 are the same query. 3 and 4 are the same query. 5 is equivalent to an inner join.

There is no "better", just correct for what you need returned.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
thank you

all queries its ok?

in some queries time is 0.0004 sec or 0.0005 sec, and some is 0.0006 sec

0.0004 sec is good?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Here's the important thing to understand: they return different results! Using a query that has shorter execution time but gives you the wrong results doesn't help you. Also, the execution time depends on the processor speed, indexes number of records in each table, number of records that match, etc, etc, etc, etc, etc.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Plus, there is no real difference between 0.0004 and 0.0006
Maybe while you were doing the second query, your os was doing something, thus it take more time, and had nothing to do with the real query time.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users