Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
mysql problem with joining 2 tables query
Old 08-17-2010, 10:34 PM mysql problem with joining 2 tables query
Experienced Talker

Posts: 32
Name: Mahmoud M. Abdel-Fattah
Location: Alexandria, Egypt.
Trades: 0
I've a problem with selecting data from 2 big mysql tables, you can find below the queries & tables details
Query 1:
It takes : 394 seconds.
Code:
SELECT SQL_CALC_FOUND_ROWS `table1`.`url`, `table1`.`Title`, `table1`.`Description`, `table2`.`col16`, `table2`.`col17`, `table2`.`col18`, `table2`.`col19`
FROM `table1`, `table2` 
WHERE `table1`.`url` = `table2`.`url` 
AND `topic` = 'Arts' 
LIMIT 0, 10
Query 2:
It takes : 0.09 seconds (which is MUCH BETTER, but the ids doesn't match in all rows, because some URLs are duplicated in table 1, but all urls are unique in table 2 . So, I can't use it)
Code:
SELECT SQL_CALC_FOUND_ROWS `table1`.`url`, `table1`.`Title`, `table1`.`Description`, `table2`.`col16`, `table2`.`col17`, `table2`.`col18`, `table2`.`col19`
FROM `table1`, `table2` 
WHERE `table1`.`id` = `table2`.`id` 
AND `topic` = 'Arts' 
LIMIT 0, 10
Table 1 "3,206,543 rows" :
Code:
`table1` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `url` varchar(255) character set utf8 collate utf8_bin NOT NULL,
  `Title` varchar(1024) character set utf8 collate utf8_bin NOT NULL,
  `Description` varchar(4000) character set utf8 collate utf8_bin NOT NULL,
  `topic` varchar(1024) character set utf8 collate utf8_bin NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `topic` (`topic`(333)),
  KEY `url` (`url`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
Table 2 "2,846,092 rows":
Code:
`table2` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `url` varchar(255) NOT NULL,
  `title` varchar(255) default NULL,
  `description` varchar(1024) default NULL,
  `keywords` varchar(1024) default NULL,
  `col1` varchar(1024) default NULL,
  `col2` varchar(1024) default NULL,
  `col3` varchar(1024) default NULL,
  `col4` varchar(50) default NULL,
  `col5` mediumint(8) unsigned default NULL,
  `col6` int(11) unsigned default NULL,
  `col7` int(11) unsigned default NULL,
     |
     |
     |
  `col23` int(11) unsigned default NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `url` (`url`),
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
So, what should I do now ?
m_abdelfattah is offline
Reply With Quote
View Public Profile Visit m_abdelfattah's homepage!
 
 
Register now for full access!
Old 08-18-2010, 05:24 AM Re: mysql problem with joining 2 tables query
Super Talker

Posts: 139
Name: John Davis
Trades: 0
It's wrong way to combine tables in query by varchar column. It's possible, but it's step to kill performance.
In any way url column from first table is not indexed.
I'm not sure UNIQUE KEY on url column from second table is useful. Guess there should be INDEX as well.

Second query is fast because id columns from both tables are INDEXED.
__________________
»
Please login or register to view this content. Registration is FREE
- Interactive maps for websites
»
Please login or register to view this content. Registration is FREE
for web developers
MapMaster is offline
Reply With Quote
View Public Profile Visit MapMaster's homepage!
 
Old 08-19-2010, 07:20 AM Re: mysql problem with joining 2 tables query
Experienced Talker

Posts: 32
Name: Mahmoud M. Abdel-Fattah
Location: Alexandria, Egypt.
Trades: 0
thanks MapMaster, I think I'll re-construct the 2nd table with integer primary key than the string one
Thanks again
m_abdelfattah is offline
Reply With Quote
View Public Profile Visit m_abdelfattah's homepage!
 
Old 08-19-2010, 08:02 PM Re: mysql problem with joining 2 tables query
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
1st, what mapmaster told you is VERY valid.
2nd, do you have indexe(s) created on table1.url and table2.url ?
This could help dramatically.
Code:
create index t1_url on table1(url);
create index t2_url on table2(url);
Try again with those.

3rd, what is the result of this query, that first compute a list of unique url's from table1 and then join to table2?
Code:
SELECT 
  SQL_CALC_FOUND_ROWS 
  t1.url, t1p.Title, t1p.Description, t2.col16, t2.col17, t2.col18, t2.col19
FROM (
  select url, max(id) as id
  from table1
  where topic = 'Arts'
  group by url
) as t1
  inner join table1 t1p  --join again to get Title and Description
    on t1.url=t1p.url
    and t1.id=t1p.id
  inner join table2 t2 
    on t1.url=t2.url
LIMIT 0, 10
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to mysql problem with joining 2 tables query
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13466 seconds with 12 queries