I am trying to achieve displaying a field in my database.
basically at the moment i have
'SELECT
historylist.artist,
historylist.ID,
artistlist.lyrics,
artistlist.ID
FROM historylist
INNER JOIN artistlist
ON historylist.ID = artistlist.ID
ORDER BY historylist.date_played DESC
LIMIT 1;'
Now this is not correct. I need to use the history list ID to link the artistlist ID. Then grab the field artistlist.lyrics. then display it. Right now when i do it like that it shows the lyrics field but its null. So i am guessing its searching historylist table
CREATE TABLE `historylist` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`songID` int(11) NOT NULL DEFAULT '0',
`filename` varchar(255) NOT NULL DEFAULT '',
`date_played` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`duration` int(11) NOT NULL DEFAULT '0',
`artist` varchar(255) NOT NULL DEFAULT '',
`title` varchar(255) NOT NULL DEFAULT '',
`album` varchar(255) NOT NULL DEFAULT '',
`albumyear` varchar(4) NOT NULL DEFAULT '',
`website` varchar(255) NOT NULL DEFAULT '',
`buycd` varchar(255) NOT NULL DEFAULT '',
`picture` varchar(255) NOT NULL DEFAULT '',
`listeners` mediumint(9) NOT NULL DEFAULT '0',
`label` varchar(100) NOT NULL DEFAULT '',
`pline` varchar(50) NOT NULL DEFAULT '',
`trackno` int(11) NOT NULL DEFAULT '0',
`composer` varchar(100) NOT NULL DEFAULT '',
`ISRC` varchar(50) NOT NULL DEFAULT '',
`catalog` varchar(50) NOT NULL DEFAULT '',
`UPC` varchar(50) NOT NULL DEFAULT '',
`feeagency` varchar(20) NOT NULL DEFAULT '',
PRIMARY KEY (`ID`),
KEY `date_played` (`date_played`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
that code above is the historylist table
this one is the
CREATE TABLE IF NOT EXISTS `artistlist` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`filename` varchar(255) NOT NULL DEFAULT '',
`diskID` int(11) NOT NULL DEFAULT '0',
`flags` varchar(10) NOT NULL DEFAULT 'NNNNNNNNNN',
`songtype` char(1) NOT NULL DEFAULT 'S',
`status` tinyint(4) NOT NULL DEFAULT '0',
`weight` double NOT NULL DEFAULT '50',
`balance` double NOT NULL DEFAULT '0',
`date_added` datetime DEFAULT NULL,
`date_played` datetime DEFAULT NULL,
`date_artist_played` datetime DEFAULT '2002-01-01 00:00:01',
`date_album_played` datetime DEFAULT '2002-01-01 00:00:01',
`date_title_played` datetime DEFAULT '2002-01-01 00:00:01',
`duration` int(11) NOT NULL DEFAULT '0',
`artist` varchar(255) NOT NULL DEFAULT '',
`title` varchar(255) NOT NULL DEFAULT '',
`album` varchar(255) NOT NULL DEFAULT '',
`label` varchar(255) NOT NULL DEFAULT '',
`pline` varchar(50) NOT NULL DEFAULT '',
`trackno` int(11) NOT NULL DEFAULT '0',
`composer` varchar(100) NOT NULL DEFAULT '',
`ISRC` varchar(50) NOT NULL DEFAULT '',
`catalog` varchar(50) NOT NULL DEFAULT '',
`UPC` varchar(50) NOT NULL DEFAULT '',
`feeagency` varchar(20) NOT NULL DEFAULT '',
`albumyear` varchar(4) NOT NULL DEFAULT '0',
`genre` varchar(20) NOT NULL DEFAULT '',
`website` varchar(255) NOT NULL DEFAULT '',
`buycd` varchar(255) NOT NULL DEFAULT '',
`info` text,
`lyrics` text,
`picture` varchar(255) NOT NULL DEFAULT '',
`count_played` mediumint(9) NOT NULL DEFAULT '0',
`count_requested` mediumint(9) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
UNIQUE KEY `filename` (`filename`),
KEY `date_played` (`date_played`),
KEY `date_artist_played` (`date_artist_played`),
KEY `date_album_played` (`date_album_played`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=347 ;
Now basically on every song i can show the data making the call to history list. but i need to use the historylist id its pulling. to then connect to the artistlist id so it pulls the lyrics field
hope that helps more
Seems like your join condition is incorrect.
ON historylist.ID = artistlist.ID
If this to be work both table have to have 1-1 relationship. Looking at your table structure i think correct condition would be
ON historylist.artist = artistlist.artist
Related
I’m trying to create a query in mySQL to select data from pre-existing tables on a database (Moodle to be specific). I realise that the scheme isn’t great, but this has come from the Moodle database with the ‘ratings’ plugin installed.
The current data structure looks like this:
mdl_ranking_points
CREATE TABLE `mdl_ranking_points` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`userid` bigint(10) NOT NULL,
`courseid` bigint(10) NOT NULL,
`points` decimal(10,1) NOT NULL,
`timecreated` bigint(10) NOT NULL,
`timemodified` bigint(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='Points of users'
mdl_ranking_logs
CREATE TABLE `mdl_ranking_logs` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`rankingid` bigint(10) NOT NULL,
`courseid` bigint(10) NOT NULL,
`course_modules_completion` bigint(10) NOT NULL,
`points` decimal(10,1) NOT NULL,
`timecreated` bigint(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8 COMMENT='Points of users'
mdl_user
CREATE TABLE `mdl_user` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`auth` varchar(20) NOT NULL DEFAULT 'manual',
`confirmed` tinyint(1) NOT NULL DEFAULT '0',
`username` varchar(100) NOT NULL DEFAULT '',
`password` varchar(255) NOT NULL DEFAULT '',
`idnumber` varchar(255) NOT NULL DEFAULT '',
`firstname` varchar(100) NOT NULL DEFAULT '',
`lastname` varchar(100) NOT NULL DEFAULT '',
`email` varchar(100) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `mdl_user_mneuse_uix` (`mnethostid`,`username`),
KEY `mdl_user_fir_ix` (`firstname`),
KEY `mdl_user_las_ix` (`lastname`),
KEY `mdl_user_idn_ix` (`idnumber`),
) ENGINE=InnoDB AUTO_INCREMENT=1045 DEFAULT CHARSET=utf8 COMMENT='One record for each person'
mdl_course
CREATE TABLE `mdl_course` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`category` bigint(10) NOT NULL DEFAULT '0',
`sortorder` bigint(10) NOT NULL DEFAULT '0',
`fullname` varchar(254) NOT NULL DEFAULT '',
`shortname` varchar(255) NOT NULL DEFAULT '',
`idnumber` varchar(100) NOT NULL DEFAULT '',
`summary` longtext,
`summaryformat` tinyint(2) NOT NULL DEFAULT '0',
`format` varchar(21) NOT NULL DEFAULT 'topics',
`showgrades` tinyint(2) NOT NULL DEFAULT '1',
`newsitems` mediumint(5) NOT NULL DEFAULT '1',
`startdate` bigint(10) NOT NULL DEFAULT '0',
`enddate` bigint(10) NOT NULL DEFAULT '0',
`marker` bigint(10) NOT NULL DEFAULT '0',
`maxbytes` bigint(10) NOT NULL DEFAULT '0',
`legacyfiles` smallint(4) NOT NULL DEFAULT '0',
`showreports` smallint(4) NOT NULL DEFAULT '0',
`visible` tinyint(1) NOT NULL DEFAULT '1',
`visibleold` tinyint(1) NOT NULL DEFAULT '1',
`groupmode` smallint(4) NOT NULL DEFAULT '0',
`groupmodeforce` smallint(4) NOT NULL DEFAULT '0',
`defaultgroupingid` bigint(10) NOT NULL DEFAULT '0',
`lang` varchar(30) NOT NULL DEFAULT '',
`theme` varchar(50) NOT NULL DEFAULT '',
`timecreated` bigint(10) NOT NULL DEFAULT '0',
`timemodified` bigint(10) NOT NULL DEFAULT '0',
`requested` tinyint(1) NOT NULL DEFAULT '0',
`enablecompletion` tinyint(1) NOT NULL DEFAULT '0',
`completionnotify` tinyint(1) NOT NULL DEFAULT '0',
`cacherev` bigint(10) NOT NULL DEFAULT '0',
`calendartype` varchar(30) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `mdl_cour_cat_ix` (`category`),
KEY `mdl_cour_idn_ix` (`idnumber`),
KEY `mdl_cour_sho_ix` (`shortname`),
KEY `mdl_cour_sor_ix` (`sortorder`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COMMENT='Central course table';
I need to return a query with the following data:
lastname | points | average
I've tried the following query, but this doesnt return what I need (it seems that it only counts user id's that have been awarded points.
SELECT lastname AS academy,
SUM(points) AS score,
COUNT(*) AS users,
(SUM(points)/COUNT(*)) AS norm
FROM mdl_ranking_points r
LEFT JOIN mdl_user ON r.userid = mdl_user.id
LEFT JOIN mdl_course ON r.courseid = mdl_course.id
GROUP BY lastname
ORDER BY norm DESC
Any help would be much appreciated. I may be approaching this completely the wrong way.
I've tried the following query, but this doesnt return what I need (it seems that it only counts user id's that have been awarded points.
This is because of the order in the joins. This will return all users even if they don't have rewarded some points
FROM mdl_user
LEFT OUTER JOIN mdl_ranking_points
LEFT OUTER JOIN mdl_course
Or change your query with a right outer join.
I have such tables:
CREATE TABLE `skadate_newsfeed_action` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`entityId` int(11) NOT NULL,
`entityType` varchar(100) NOT NULL,
`feature` varchar(100) NOT NULL,
`data` longtext NOT NULL,
`status` varchar(20) NOT NULL DEFAULT 'active',
`createTime` int(11) NOT NULL,
`updateTime` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`visibility` int(11) NOT NULL,
`privacy` enum('everybody','friends_only') NOT NULL DEFAULT 'everybody',
PRIMARY KEY (`id`),
KEY `userId` (`userId`),
KEY `privacy` (`visibility`),
KEY `updateTime` (`updateTime`),
KEY `entity` (`entityType`,`entityId`)
) ENGINE=MyISAM;
CREATE TABLE `skadate_profile` (
`profile_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(128) NOT NULL DEFAULT '',
`username` varchar(32) NOT NULL DEFAULT '',
`password` varchar(40) NOT NULL,
`sex` bigint(20) DEFAULT NULL,
`match_sex` bigint(20) DEFAULT NULL,
`birthdate` date NOT NULL DEFAULT '0000-00-00',
`headline` varchar(128) DEFAULT '',
`general_description` text,
`match_agerange` varchar(6) DEFAULT NULL,
`custom_location` varchar(255) DEFAULT NULL,
`country_id` char(2) NOT NULL DEFAULT '',
`zip` varchar(10) DEFAULT NULL,
`state_id` varchar(5) DEFAULT NULL,
`city_id` int(11) DEFAULT '0',
`join_stamp` int(10) unsigned NOT NULL DEFAULT '0',
`activity_stamp` int(10) unsigned NOT NULL DEFAULT '0',
`membership_type_id` int(10) unsigned NOT NULL DEFAULT '18',
`affiliate_id` int(8) unsigned NOT NULL DEFAULT '0',
`email_verified` enum('undefined','yes','no') NOT NULL DEFAULT 'undefined',
`reviewed` enum('n','y') NOT NULL DEFAULT 'n',
`has_photo` enum('n','y') NOT NULL DEFAULT 'n',
`has_media` enum('n','y') NOT NULL DEFAULT 'n',
`status` enum('active','on_hold','suspended') NOT NULL DEFAULT 'active',
`featured` enum('n','y') NOT NULL DEFAULT 'n',
`register_invite_score` tinyint(3) NOT NULL DEFAULT '0',
`rate_score` tinyint(3) unsigned NOT NULL DEFAULT '0',
`rates` bigint(20) unsigned NOT NULL DEFAULT '0',
`language_id` int(10) unsigned NOT NULL DEFAULT '0',
`join_ip` int(11) unsigned NOT NULL DEFAULT '0',
`neigh_location` enum('country','state','city','zip') DEFAULT NULL,
`neigh_location_distance` int(10) unsigned NOT NULL DEFAULT '0',
`bg_color` varchar(32) DEFAULT NULL,
`bg_image` varchar(32) DEFAULT NULL,
`bg_image_url` varchar(255) DEFAULT NULL,
`bg_image_mode` tinyint(1) DEFAULT NULL,
`bg_image_status` enum('active','approval') NOT NULL DEFAULT 'active',
`has_music` enum('n','y') NOT NULL DEFAULT 'n',
`is_private` tinyint(1) NOT NULL DEFAULT '0',
`subscription_id_offerit` text,
PRIMARY KEY (`profile_id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `username` (`username`),
KEY `membership_id` (`membership_type_id`),
KEY `zip` (`zip`),
KEY `country_id` (`country_id`),
KEY `state_id` (`state_id`),
KEY `city_id` (`city_id`),
KEY `sex` (`sex`),
KEY `match_sex` (`match_sex`),
KEY `activity_stamp` (`activity_stamp`),
KEY `join_stamp` (`join_stamp`),
KEY `birthdate` (`birthdate`),
KEY `featured` (`featured`,`has_photo`,`activity_stamp`)
) ENGINE=MyISAM;
And try to perform this query:
SELECT DISTINCT `na`.*
FROM `skadate_newsfeed_action` AS `na`
LEFT JOIN `skadate_profile` AS `profile` ON ( `na`.`userId` = `profile`.`profile_id` )
WHERE ( profile.email_verified='yes' OR profile.email_verified='no' OR profile.email_verified='undefined' )
AND `profile`.`status`='active' AND `na`.`status`='active' AND `na`.`privacy`='everybody'
AND `na`.`visibility` & 1 AND `na`.`updateTime` < 1455885224
ORDER BY `na`.`updateTime` DESC, `na`.`id` DESC
LIMIT 0, 10
But when I see EXPLAIN:
Maybe someone can help me, how I can improve this query?
If you want records from only one table, then use exists rather than a join and select distinct. So:
SELECT na.*
FROM `skadate_newsfeed_action` na
WHERE EXISTS (SELECT 1
FROM skadate_profile p
WHERE na.userId = p.profile_id AND
p.email_verified IN ('yes', 'no', 'undefined') AND
p.status = 'active'
) AND
na.status = 'active' AND
na.privacy = 'everybody' AND
na.visibility & 1 > 0 AND
na.updateTime < 1455885224
ORDER BY na.`updateTime` DESC, na.`id` DESC
LIMIT 0, 10;
For this query, you want an index on skadate_profile(profile_id, status, verified). Also, the following index is probably helpful: skadate_newsfeed_action(status, privacy, updateTime, visibility, userId).
This is probably because of the DISTICT keyword. To remove duplicates MySQL needs to sort the result by every selected column.
please could someone tell me the problem with this syntax because mysql 5.5.32 keeps tell me about an error
CREATE TABLE `clients` (
`ID` tinyint(11) NOT NULL auto_increment,
`title` varchar(10) NOT NULL default '',
`firstName` varchar(30) NOT NULL default '',
`lastName` varchar(30) NOT NULL default '',
`address1` varchar(100) NOT NULL default '',
`address2` varchar(100) NOT NULL default '',
`town` varchar(100) NOT NULL default '',
`province` varchar(100) NOT NULL default '',
`country` varchar(40) NOT NULL default '',
`postCode` varchar(20) NOT NULL default '',
`telephone` varchar(20) NOT NULL default '',
`email` varchar(100) NOT NULL default '',
`cardNo` varchar(16) NOT NULL default '0000-00-00',
`expiryDate` date NOT NULL default '0000-00-00',
PRIMARY KEY (`ID`)
) TYPE=MyISAM COMMENT='customer table' AUTO_INCREMENT=1 ;
The keyword TYPE has been replaced by ENGINE as in
ENGINE=MyISAM
change TYPE to ENGINE like this:
CREATE TABLE `clients` (
`ID` tinyint(11) NOT NULL auto_increment,
`title` varchar(10) NOT NULL default '',
`firstName` varchar(30) NOT NULL default '',
`lastName` varchar(30) NOT NULL default '',
`address1` varchar(100) NOT NULL default '',
`address2` varchar(100) NOT NULL default '',
`town` varchar(100) NOT NULL default '',
`province` varchar(100) NOT NULL default '',
`country` varchar(40) NOT NULL default '',
`postCode` varchar(20) NOT NULL default '',
`telephone` varchar(20) NOT NULL default '',
`email` varchar(100) NOT NULL default '',
`cardNo` varchar(16) NOT NULL default '0000-00-00',
`expiryDate` date NOT NULL default '0000-00-00',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM COMMENT='customer table' AUTO_INCREMENT=1 ;
MySQL 5.0 accepts TYPE or ENGINE, but above MySQL 5.1, Only ENGINE is allowed.
The idea is quite simple: i have three (or more) tables
- master_tbl (id, something, somethingelse)
- tbl2 (id, ....)
- tbl3 (id, ....)
Now what i want is a foreign key relationship, such as tbl3.id would point to tbl2.id and tbl2.id would point to master_tbl.id - all foreign keys are ON UPDATE CASCADE and ON DELETE CASCADE. What I'll get from this is that when I delete a record from tbl2, its tbl3 equivalent will get erased as well, but not master_tbl. When I delete a record from master_tbl, all three tables get erased.
When I try to create the foreign key on tbl3.id->tbl2.id, I get mysql error 150 - can't create table (the tbl2.id->master_tbl.id is already created).
My MySQL version is 5.1.46. Any ideas why this might be?
EDIT: table definitions
smf_members aka master_table
-- Table "smf_members" DDL
CREATE TABLE `smf_members` (
`id_member` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`member_name` varchar(80) NOT NULL DEFAULT '',
`date_registered` int(10) unsigned NOT NULL DEFAULT '0',
`posts` mediumint(8) unsigned NOT NULL DEFAULT '0',
`id_group` smallint(5) unsigned NOT NULL DEFAULT '0',
`lngfile` varchar(255) NOT NULL DEFAULT '',
`last_login` int(10) unsigned NOT NULL DEFAULT '0',
`real_name` varchar(255) NOT NULL DEFAULT '',
`instant_messages` smallint(5) NOT NULL DEFAULT '0',
`unread_messages` smallint(5) NOT NULL DEFAULT '0',
`new_pm` tinyint(3) unsigned NOT NULL DEFAULT '0',
`buddy_list` text NOT NULL,
`pm_ignore_list` varchar(255) NOT NULL DEFAULT '',
`pm_prefs` mediumint(8) NOT NULL DEFAULT '0',
`mod_prefs` varchar(20) NOT NULL DEFAULT '',
`message_labels` text NOT NULL,
`passwd` varchar(64) NOT NULL DEFAULT '',
`openid_uri` text NOT NULL,
`email_address` varchar(255) NOT NULL DEFAULT '',
`personal_text` varchar(255) NOT NULL DEFAULT '',
`gender` tinyint(4) unsigned NOT NULL DEFAULT '0',
`birthdate` date NOT NULL DEFAULT '0001-01-01',
`website_title` varchar(255) NOT NULL DEFAULT '',
`website_url` varchar(255) NOT NULL DEFAULT '',
`location` varchar(255) NOT NULL DEFAULT '',
`icq` varchar(255) NOT NULL DEFAULT '',
`aim` varchar(255) NOT NULL DEFAULT '',
`yim` varchar(32) NOT NULL DEFAULT '',
`msn` varchar(255) NOT NULL DEFAULT '',
`hide_email` tinyint(4) NOT NULL DEFAULT '0',
`show_online` tinyint(4) NOT NULL DEFAULT '1',
`time_format` varchar(80) NOT NULL DEFAULT '',
`signature` text NOT NULL,
`time_offset` float NOT NULL DEFAULT '0',
`avatar` varchar(255) NOT NULL DEFAULT '',
`pm_email_notify` tinyint(4) NOT NULL DEFAULT '0',
`karma_bad` smallint(5) unsigned NOT NULL DEFAULT '0',
`karma_good` smallint(5) unsigned NOT NULL DEFAULT '0',
`usertitle` varchar(255) NOT NULL DEFAULT '',
`notify_announcements` tinyint(4) NOT NULL DEFAULT '1',
`notify_regularity` tinyint(4) NOT NULL DEFAULT '1',
`notify_send_body` tinyint(4) NOT NULL DEFAULT '0',
`notify_types` tinyint(4) NOT NULL DEFAULT '2',
`member_ip` varchar(255) NOT NULL DEFAULT '',
`member_ip2` varchar(255) NOT NULL DEFAULT '',
`secret_question` varchar(255) NOT NULL DEFAULT '',
`secret_answer` varchar(64) NOT NULL DEFAULT '',
`id_theme` tinyint(4) unsigned NOT NULL DEFAULT '0',
`is_activated` tinyint(3) unsigned NOT NULL DEFAULT '1',
`validation_code` varchar(10) NOT NULL DEFAULT '',
`id_msg_last_visit` int(10) unsigned NOT NULL DEFAULT '0',
`additional_groups` varchar(255) NOT NULL DEFAULT '',
`smiley_set` varchar(48) NOT NULL DEFAULT '',
`id_post_group` smallint(5) unsigned NOT NULL DEFAULT '0',
`total_time_logged_in` int(10) unsigned NOT NULL DEFAULT '0',
`password_salt` varchar(255) NOT NULL DEFAULT '',
`ignore_boards` text NOT NULL,
`warning` tinyint(4) NOT NULL DEFAULT '0',
`passwd_flood` varchar(12) NOT NULL DEFAULT '',
`pm_receive_from` tinyint(4) unsigned NOT NULL DEFAULT '1',
PRIMARY KEY (`id_member`),
KEY `member_name` (`member_name`),
KEY `real_name` (`real_name`),
KEY `date_registered` (`date_registered`),
KEY `id_group` (`id_group`),
KEY `birthdate` (`birthdate`),
KEY `posts` (`posts`),
KEY `last_login` (`last_login`),
KEY `lngfile` (`lngfile`(30)),
KEY `id_post_group` (`id_post_group`),
KEY `warning` (`warning`),
KEY `total_time_logged_in` (`total_time_logged_in`),
KEY `id_theme` (`id_theme`)
) ENGINE=InnoDB AUTO_INCREMENT=73 DEFAULT CHARSET=utf8;
cyp_users aka tbl2
-- Table "cyp_users" DDL
CREATE TABLE `cyp_users` (
`id` mediumint(8) unsigned NOT NULL,
`role` varchar(255) NOT NULL DEFAULT 'unregistered',
PRIMARY KEY (`id`),
CONSTRAINT `cyp_users_ibfk_1` FOREIGN KEY (`id`) REFERENCES `smf_members` (`id_member`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
cyp_vip_users aka tbl3
-- Table "cyp_vip_users" DDL
CREATE TABLE `cyp_vip_users` (
`id` mediumint(8) NOT NULL,
`od` date NOT NULL,
`do` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The ID's of tbl2 and tbl3 are different types.
Foreign Keys need to be of the same type.
tbl2.ID is UNSIGNED
tbl3.ID is SIGNED
using latest mysql server,
after some period of inactivity my website sqls times out (only some not all)
these queries are not newly written existing queries.
Testing results
when execute a simple sql like (select count(*) from products) this works fine all the time.
when execute below sql
SELECT products.pid
FROM
products INNER JOIN
catalog ON products.cid=catalog.cid
WHERE
products.is_visible = 'Yes' AND (
products.inventory_control = 'No' OR
products.stock > 0 OR
products.inventory_rule = 'OutOfStock' OR (
products.inventory_control = 'AttrRuleInc' AND
products.stock >= 0
)
) AND products.is_home = 'Yes'
GROUP BY products.pid
it times out ( Note it will not timeout all the time, this happens after 1 hour or 2 hour of inactivity)
The very first execution takes 30+ seconds and it times out and after that the above executing SQL 2 to 3 times the 4th time onwards it executes fast in 3 to 5 seconds
after 1 or 2 hour inactivity this pattern repeats.
I did not do any changes in settings.
i have 2 sites on this server.
1st server has 20,000 rows of in database ( this one works fine ) 2nd site has 150,000 rows in the databse ( this one is having this issue)
So this is something "query time out"
i do not think this is something on SQL settings, if it is then i should see this behavior on both sites
**** here is the table structure for which the issue with timeout / taking 40 seconds
#this structure has 2 additional keys we created
# KEY `product_id` (`product_id`),
# KEY `product_no` (`product_no`)
# this one is having issues
--
-- Table structure for table `products` on lpbatt database server
--
DROP TABLE IF EXISTS `products`;
SET #saved_cs_client = ##character_set_client;
SET character_set_client = utf8;
CREATE TABLE `products` (
`pid` int(10) unsigned NOT NULL auto_increment,
`cid` int(10) unsigned NOT NULL default '0',
`manufacturer_id` int(10) unsigned NOT NULL default '0',
`is_visible` enum('Yes','No') NOT NULL default 'Yes',
`is_hotdeal` enum('Yes','No') NOT NULL default 'No',
`is_home` enum('Yes','No') NOT NULL default 'No',
`is_taxable` enum('Yes','No') NOT NULL default 'Yes',
`is_dollar_days` enum('Yes','No') NOT NULL default 'No',
`is_google_co` enum('Yes','No') NOT NULL default 'Yes',
`is_doba` enum('Yes','No') NOT NULL default 'No',
`is_locked` enum('Yes','No') NOT NULL default 'No',
`inventory_control` enum('Yes','AttrRuleExc','AttrRuleInc','No') NOT NULL default 'No',
`inventory_rule` enum('Hide','OutOfStock') NOT NULL default 'Hide',
`stock` int(10) NOT NULL default '0',
`stock_warning` int(10) NOT NULL default '0',
`weight` decimal(10,2) unsigned NOT NULL default '0.00',
`free_shipping` enum('Yes','No') NOT NULL default 'No',
`digital_product` enum('Yes','No') NOT NULL default 'No',
`digital_product_file` varchar(255) NOT NULL default '',
`cost` decimal(20,5) unsigned NOT NULL default '0.00000',
`price` decimal(20,5) unsigned NOT NULL default '0.00000',
`price2` decimal(20,5) unsigned NOT NULL default '0.00000',
`price_level_1` decimal(20,5) unsigned NOT NULL default '0.00000',
`price_level_2` decimal(20,5) unsigned NOT NULL default '0.00000',
`price_level_3` decimal(20,5) unsigned NOT NULL default '0.00000',
`shipping_price` decimal(20,5) unsigned NOT NULL default '0.00000',
`tax_class_id` int(10) unsigned NOT NULL default '0',
`tax_rate` decimal(20,5) NOT NULL default '-1.00000',
`call_for_price` enum('Yes','No') NOT NULL default 'No',
`priority` int(11) NOT NULL default '0',
`attributes_count` int(11) NOT NULL default '0',
`min_order` int(10) NOT NULL default '1',
`max_order` int(10) unsigned NOT NULL default '0',
`added` datetime NOT NULL default '0000-00-00 00:00:00',
`products_location_id` int(10) unsigned NOT NULL default '0',
`url_hash` varchar(32) NOT NULL default '',
`url_default` varchar(128) NOT NULL default '',
`url_custom` varchar(128) NOT NULL default '',
`product_id` int(64) NOT NULL default '0',
`product_sku` varchar(64) NOT NULL default '',
`product_upc` varchar(64) NOT NULL default '',
`case_pack` int(11) NOT NULL default '-1',
`inter_pack` int(11) NOT NULL default '-1',
`gift_quantity` int(10) unsigned NOT NULL default '0',
`dimension_width` decimal(10,2) NOT NULL default '0.00',
`dimension_length` decimal(10,2) NOT NULL default '0.00',
`dimension_height` decimal(10,2) NOT NULL default '0.00',
`image_location` enum('Local','Web') NOT NULL default 'Local',
`image_url` varchar(255) NOT NULL default '',
`image_alt_text` varchar(255) NOT NULL default '',
`tmp_manufacturer` varchar(30) default NULL,
`tmp_family` varchar(30) default NULL,
`tmp_series` varchar(30) default NULL,
`tmp_model` varchar(30) default NULL,
`tmp_ptype` varchar(30) default NULL,
`product_no` varchar(40) default NULL,
`part_no` varchar(300) default NULL,
`spec_1` varchar(7) default NULL,
`spec_2` varchar(7) default NULL,
`spec_3` varchar(7) default NULL,
`spec_4` varchar(40) default NULL,
`title` varchar(255) NOT NULL default '',
`meta_keywords` text NOT NULL,
`meta_title` text NOT NULL,
`meta_description` text NOT NULL,
`overview` text,
`description` text,
`zoom_option` enum('global','none','zoom','magnify','magicthumb','imagelayover') NOT NULL default 'global',
PRIMARY KEY (`pid`),
KEY `cid` (`cid`),
KEY `is_visible` (`is_visible`),
KEY `url_hash` (`url_hash`),
KEY `product_id` (`product_id`),
KEY `product_no` (`product_no`)
) ENGINE=MyISAM AUTO_INCREMENT=1630746530 DEFAULT CHARSET=utf8;
SET character_set_client = #saved_cs_client;
below is the table structure which is running on another server but working fine
#see the products table structure and catalog table structure
#this is fine on this server
# --------------------------------------------------------
# Host: laptopnbparts.com
# Database: laptopnbpartscom
# Server version: 5.0.77
# Server OS: redhat-linux-gnu
# HeidiSQL version: 5.0.0.3222
# Date/time: 2010-06-25 18:13:33
# --------------------------------------------------------
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
# Dumping structure for table laptopnbpartscom.catalog
CREATE TABLE IF NOT EXISTS `catalog` (
`cid` int(10) unsigned NOT NULL auto_increment,
`parent` int(10) unsigned NOT NULL default '0',
`level` int(10) unsigned NOT NULL default '0',
`priority` smallint(5) unsigned NOT NULL default '5',
`is_visible` enum('Yes','No') NOT NULL default 'Yes',
`list_subcats` enum('Yes','No') NOT NULL default 'No',
`url_hash` varchar(32) NOT NULL default '',
`url_default` varchar(128) NOT NULL default '',
`url_custom` varchar(128) NOT NULL default '',
`key_name` varchar(255) NOT NULL default '',
`meta_keywords` text,
`meta_title` text,
`meta_description` text,
`category_header` varchar(255) NOT NULL default '',
`name` varchar(255) NOT NULL default '',
`description` text,
`description_bottom` text,
`category_path` text,
PRIMARY KEY (`cid`),
KEY `parent` (`parent`),
KEY `level` (`level`),
KEY `priority` (`priority`),
KEY `url_hash` (`url_hash`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
# Data exporting was unselected.
# Dumping structure for table laptopnbpartscom.products
CREATE TABLE IF NOT EXISTS `products` (
`pid` int(10) unsigned NOT NULL auto_increment,
`cid` int(10) unsigned NOT NULL default '0',
`manufacturer_id` int(10) unsigned NOT NULL default '0',
`is_visible` enum('Yes','No') NOT NULL default 'Yes',
`is_hotdeal` enum('Yes','No') NOT NULL default 'No',
`is_home` enum('Yes','No') NOT NULL default 'No',
`is_taxable` enum('Yes','No') NOT NULL default 'Yes',
`is_dollar_days` enum('Yes','No') NOT NULL default 'No',
`is_google_co` enum('Yes','No') NOT NULL default 'Yes',
`is_doba` enum('Yes','No') NOT NULL default 'No',
`is_locked` enum('Yes','No') NOT NULL default 'No',
`inventory_control` enum('Yes','AttrRuleExc','AttrRuleInc','No') NOT NULL default 'No',
`inventory_rule` enum('Hide','OutOfStock') NOT NULL default 'Hide',
`stock` int(10) NOT NULL default '0',
`stock_warning` int(10) NOT NULL default '0',
`weight` decimal(10,2) unsigned NOT NULL default '0.00',
`free_shipping` enum('Yes','No') NOT NULL default 'No',
`digital_product` enum('Yes','No') NOT NULL default 'No',
`digital_product_file` varchar(255) NOT NULL default '',
`cost` decimal(20,5) unsigned NOT NULL default '0.00000',
`price` decimal(20,5) unsigned NOT NULL default '0.00000',
`price2` decimal(20,5) unsigned NOT NULL default '0.00000',
`price_level_1` decimal(20,5) unsigned NOT NULL default '0.00000',
`price_level_2` decimal(20,5) unsigned NOT NULL default '0.00000',
`price_level_3` decimal(20,5) unsigned NOT NULL default '0.00000',
`shipping_price` decimal(20,5) unsigned NOT NULL default '0.00000',
`tax_class_id` int(10) unsigned NOT NULL default '0',
`tax_rate` decimal(20,5) NOT NULL default '-1.00000',
`call_for_price` enum('Yes','No') NOT NULL default 'No',
`priority` int(11) NOT NULL default '0',
`attributes_count` int(11) NOT NULL default '0',
`min_order` int(10) NOT NULL default '1',
`max_order` int(10) unsigned NOT NULL default '0',
`added` datetime NOT NULL default '0000-00-00 00:00:00',
`products_location_id` int(10) unsigned NOT NULL default '0',
`url_hash` varchar(32) NOT NULL default '',
`url_default` varchar(128) NOT NULL default '',
`url_custom` varchar(128) NOT NULL default '',
`product_id` varchar(64) NOT NULL default '',
`product_sku` varchar(64) NOT NULL default '',
`product_upc` varchar(64) NOT NULL default '',
`case_pack` int(11) NOT NULL default '-1',
`inter_pack` int(11) NOT NULL default '-1',
`gift_quantity` int(10) unsigned NOT NULL default '0',
`dimension_width` decimal(10,2) NOT NULL default '0.00',
`dimension_length` decimal(10,2) NOT NULL default '0.00',
`dimension_height` decimal(10,2) NOT NULL default '0.00',
`image_location` enum('Local','Web') NOT NULL default 'Local',
`image_url` varchar(255) NOT NULL default '',
`image_alt_text` varchar(255) NOT NULL default '',
`tmp_manufacturer` varchar(30) default NULL,
`tmp_series` varchar(30) default NULL,
`tmp_model` varchar(30) default NULL,
`tmp_ptype` varchar(30) default NULL,
`product_no` varchar(60) default NULL,
`part_no` varchar(60) default NULL,
`watt_volt_amp` varchar(100) default NULL,
`title` varchar(255) NOT NULL default '',
`meta_keywords` text NOT NULL,
`meta_title` text NOT NULL,
`meta_description` text NOT NULL,
`overview` text,
`description` text,
`zoom_option` enum('global','none','zoom','magnify','magicthumb','imagelayover') NOT NULL default 'global',
PRIMARY KEY (`pid`),
KEY `cid` (`cid`),
KEY `is_visible` (`is_visible`),
KEY `url_hash` (`url_hash`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
# Data exporting was unselected.
/*!40101 SET SQL_MODE=#OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
Try using CASE WHEN instead of the multiple OR statements in the WHERE clause and evaluate on the client side.
OR is a well-known performance killer as MySQL cannot usually apply INDEXes and must check every single statement in order to filter out rows.