Auto increment it's not working - mysql

I have a problem on my website with auto increment and I can't find a way to fix it...
It works with this table:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(40) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`password` varchar(40) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`loginhash` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`id_level` int(10) NOT NULL DEFAULT '1',
`random` int(10) DEFAULT '0',
`email` varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`language` tinyint(4) NOT NULL DEFAULT '1',
`style` tinyint(4) NOT NULL DEFAULT '1',
`joined` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`lastconnect` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`lip` bigint(11) DEFAULT '0',
`downloaded` bigint(20) DEFAULT '0',
`uploaded` bigint(20) DEFAULT '0',
`avatar` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`pid` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`flag` tinyint(1) unsigned NOT NULL DEFAULT '0',
`topicsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`postsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`torrentsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`cip` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
`time_offset` varchar(4) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
`temp_email` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`flags` bigint(20) unsigned NOT NULL DEFAULT '7345036',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
KEY `id_level` (`id_level`),
KEY `pid` (`pid`),
KEY `cip` (`cip`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
But it doesn't work with this table:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(40) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`password` varchar(40) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`loginhash` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`id_level` int(10) NOT NULL DEFAULT '1',
`random` int(10) DEFAULT '0',
`email` varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`language` tinyint(4) NOT NULL DEFAULT '1',
`style` tinyint(4) NOT NULL DEFAULT '1',
`joined` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`lastconnect` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`lip` bigint(11) DEFAULT '0',
`downloaded` bigint(20) DEFAULT '0',
`uploaded` bigint(20) DEFAULT '0',
`avatar` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`pid` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`flag` tinyint(1) unsigned NOT NULL DEFAULT '0',
`topicsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`postsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`torrentsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`cip` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
`time_offset` varchar(4) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
`temp_email` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`flags` bigint(20) unsigned NOT NULL DEFAULT '7345036',
`rank_switch` enum('yes','no') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'no',
`old_rank` varchar(12) COLLATE utf8_unicode_ci NOT NULL DEFAULT '3',
`timed_rank` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`seedbonus` decimal(5,1) NOT NULL,
`on_forum` int(11) NOT NULL DEFAULT '0',
`on_topic` int(11) NOT NULL DEFAULT '0',
`custom_title` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`invites` int(10) NOT NULL DEFAULT '0',
`invited_by` int(10) NOT NULL DEFAULT '0',
`invitedate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`donor` varchar(10) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'no',
`banfromforum` enum('yes','no') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'no',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
KEY `id_level` (`id_level`),
KEY `pid` (`pid`),
KEY `cip` (`cip`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Can anyone tell me where the problem is?

Related

multiple joins SQL query optimization

I have the following query :
SELECT
COUNT(DISTINCT a0_.id) AS sclr0
FROM
account a0_
INNER JOIN customer c1_ ON (c1_.account = a0_.id)
LEFT JOIN sf_user_data s2_ ON (s2_.user_id = a0_.id)
LEFT JOIN address a3_ ON (c1_.customer_address = a3_.id)
WHERE
a3_.city IS NOT NULL
resulting in the following output :
sclr0
+--------+
298279
with the following EXPLAIN :
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
+--+---------------+-------+-----------+-------+--------------------------------------------+-----------------------+-----------+--------------------------------+-------+-----------+-------+
1 SIMPLE c1_ NULL ALL UNIQ_81398E097D3656A4,UNIQ_81398E091193CB3F NULL NULL NULL 405508 100.00 NULL
1 SIMPLE a0_ NULL eq_ref PRIMARY PRIMARY 8 evoportail.c1_.account 1 100.00 Using index
1 SIMPLE s2_ NULL eq_ref UNIQ_E904BFD1A76ED395 UNIQ_E904BFD1A76ED395 8 evoportail.c1_.account 1 100.00 Using index
1 SIMPLE a3_ NULL eq_ref PRIMARY PRIMARY 8 evoportail.c1_.customer_address 1 90.00 Using where
approximative number of rows in the tables :
account : 430000
customer: 430000
sf_user_data : 115000
address : 550000
Right now, this query is running in 3 seconds. Is there any way to speed it up ?
the CREATE statements :
CREATE TABLE `account` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`identifier` varchar(255) collate utf8_bin NOT NULL,
`hash` varchar(255) collate utf8_bin default NULL,
`date_create` datetime default NULL,
`group` varchar(50) collate utf8_bin default NULL,
`sub_group` varchar(50) collate utf8_bin NOT NULL default 'NULL',
`date_last_action` datetime default NULL,
`date_last_connection` datetime default NULL,
`connection_counter` int(10) unsigned default NULL,
`connection_since_customer` int(10) unsigned NOT NULL default '0',
`salt` varchar(255) collate utf8_bin default NULL,
`roles` longtext collate utf8_bin COMMENT '(DC2Type:array)',
`is_v3` tinyint(1) NOT NULL default '1',
`password_token` varchar(255) collate utf8_bin default NULL,
`password_token_expired_at` datetime default NULL,
`is_included_in_newsletters` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`),
UNIQUE KEY `identifier_UNIQUE` (`identifier`)
) ENGINE=MyISAM AUTO_INCREMENT=434243 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `address` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`city` varchar(64) collate utf8_bin default NULL,
`street` varchar(255) collate utf8_bin default NULL,
`complement` varchar(128) collate utf8_bin default NULL,
`zipcode` varchar(16) collate utf8_bin default NULL,
`country_id` int(11) default NULL,
`cedex` tinyint(1) NOT NULL default '0',
`abroad` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `IDX_D4E6F81F92F3E70` (`country_id`)
) ENGINE=MyISAM AUTO_INCREMENT=541873 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `customer` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`account` bigint(20) unsigned NOT NULL,
`source` varchar(250) collate utf8_bin NOT NULL default 'DECLARATION',
`last_source` varchar(250) collate utf8_bin NOT NULL,
`source_domain_name` varchar(255) collate utf8_bin default NULL,
`subscription_offer` varchar(255) collate utf8_bin default NULL,
`formalities_center_address` bigint(20) unsigned default NULL,
`customer_address` bigint(20) unsigned default NULL,
`business_address` bigint(20) unsigned default NULL,
`shipping_address` bigint(20) default NULL,
`activity` text collate utf8_bin,
`state` enum('NONE','CREATE','UPDATE','COMPLETE') collate utf8_bin NOT NULL default 'NONE',
`num_dossier` varchar(255) collate utf8_bin default NULL,
`email` varchar(255) collate utf8_bin NOT NULL,
`lastname` varchar(255) collate utf8_bin NOT NULL,
`firstname` varchar(255) collate utf8_bin NOT NULL,
`sexe` int(1) NOT NULL default '0',
`activityset` int(1) NOT NULL default '0',
`phone` varchar(16) collate utf8_bin NOT NULL,
`business_name` varchar(255) collate utf8_bin default NULL,
`payment_method` enum('NONE','CB_OK','CB_KO','CHEQUE_OK','CHEQUE_KO','WAITING','IMPACTPLUS_OK','PRELEV') collate utf8_bin default 'NONE',
`payment_waiting_comment` text collate utf8_bin,
`sub_sent_recovery` smallint(6) default '0',
`transaction_number` varchar(30) collate utf8_bin default NULL,
`transaction_date` datetime default NULL,
`properties` set('ACCRE','CFE','WANT_WEBSITE','HAVE_WEBSITE','NEWSLETTER','SUBSCRIBE','OLD_CUSTOMER') collate utf8_bin default NULL,
`comments` text collate utf8_bin,
`activity_declaration` varchar(512) collate utf8_bin default NULL COMMENT 'file:///',
`cfe_center` varchar(255) collate utf8_bin default NULL,
`date_create` datetime default NULL,
`date_complete` datetime default NULL,
`date_subscribe` datetime default NULL,
`date_next_payement` datetime default NULL,
`date_ae_subscribe` datetime default NULL,
`siret` varchar(128) collate utf8_bin default NULL,
`current_quotation` bigint(20) unsigned default NULL,
`current_invoice` bigint(20) unsigned default NULL,
`has_create_quotation` tinyint(1) NOT NULL default '0',
`has_create_invoice` tinyint(1) NOT NULL default '0',
`created_by` int(20) NOT NULL default '0',
`updated_by` int(11) NOT NULL default '0',
`updated_date` datetime default NULL,
`abo_running` tinyint(1) NOT NULL default '1',
`show_bn` tinyint(1) NOT NULL default '1',
`taxe_type_activite` enum('NULL','ACHAT','SERVICE','BOTH') collate utf8_bin default NULL,
`taxe_categorie_activite` enum('NULL','COMMERCIALE','ARTISANALE','CIPAV','RSI') collate utf8_bin default NULL,
`taxe_liberatoire` enum('NULL','OUI','NON') collate utf8_bin default NULL,
`taxe_statut_accre` enum('NULL','OUI','NON','DK') collate utf8_bin default NULL,
`know` varchar(50) collate utf8_bin default NULL,
`sms_relance` int(11) default NULL,
`fdae` int(1) NOT NULL default '0',
`display_fdae` tinyint(1) NOT NULL default '0',
`show_dispense_immat` enum('RCS','RM','RSAC') collate utf8_bin default NULL,
`show_dispense_immat_city` varchar(255) collate utf8_bin default NULL,
`subscription_fdae` date default NULL,
`nbsocial` varchar(30) collate utf8_bin default NULL,
`atclic` int(1) NOT NULL default '0',
`merassurance` int(1) NOT NULL default '0',
`dossier_canceled` tinyint(1) NOT NULL default '0',
`dossier_canceled_date` datetime default NULL,
`tva_intra` varchar(20) collate utf8_bin default NULL,
`site_url` varchar(100) collate utf8_bin default NULL,
`freeguide` tinyint(1) NOT NULL default '0',
`hiscox` int(1) NOT NULL default '0',
`assurland` int(1) NOT NULL default '0',
`unpaid_advisor` int(11) default NULL,
`unpaid_date` datetime default NULL,
`ecl_send` tinyint(1) default NULL,
`ecl_date` datetime default NULL,
`birthdateyear` int(11) NOT NULL default '0',
`quaCategorie` varchar(500) collate utf8_bin default NULL,
`quaNature` varchar(500) collate utf8_bin default NULL,
`quaType` varchar(500) collate utf8_bin default NULL,
`april` int(1) NOT NULL default '0',
`date_guide` datetime default NULL,
`no_pub` tinyint(1) NOT NULL default '0',
`campaign_manual` tinyint(1) NOT NULL default '0',
`export_matmut` date default NULL,
`formality_date` datetime default NULL,
`call_count_commerciaux` int(11) default '0',
`call_count_assistance` int(11) default '0',
`call_last` datetime default NULL,
`prospect` tinyint(1) default NULL,
`gender_id` int(11) default NULL,
`birthdate` date default NULL,
`current_customer_source_history_id` int(11) default NULL,
`original_customer_source_history_id` int(11) default NULL,
`bounce` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_81398E097D3656A4` (`account`),
UNIQUE KEY `UNIQ_81398E09E7927C74` (`email`),
UNIQUE KEY `UNIQ_81398E091193CB3F` (`customer_address`),
UNIQUE KEY `UNIQ_81398E09507DD4CC` (`business_address`),
UNIQUE KEY `UNIQ_81398E09BA38C653` (`formalities_center_address`),
KEY `num_dossier` (`num_dossier`),
KEY `sub_send_recovery` (`sub_sent_recovery`),
KEY `dossier_canceled` (`dossier_canceled`),
KEY `freeguide` (`freeguide`),
KEY `IDX_81398E09708A0E0` (`gender_id`),
KEY `IDX_81398E0935655550` (`current_customer_source_history_id`),
KEY `IDX_81398E0981A1F986` (`original_customer_source_history_id`)
) ENGINE=MyISAM AUTO_INCREMENT=433026 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `sf_user_data` (
`id` int(11) NOT NULL auto_increment,
`user_id` bigint(20) unsigned NOT NULL,
`register_id` int(11) default NULL,
`insurance_id` int(11) default NULL,
`activity_started_at` date default NULL,
`accre_request_accepted` tinyint(1) default NULL,
`accre_request_accepted_at` date default NULL,
`declaration_frequency_months` smallint(6) default NULL,
`declaration_reminder` tinyint(1) default NULL,
`activities_number` smallint(6) default NULL,
`computed_main_activity_percent_total` double default NULL,
`computed_secondary_activity_percent_total` double default NULL,
`company_address_is_personal_address` tinyint(1) default NULL,
`register_city` varchar(255) collate utf8_unicode_ci default NULL,
`invoice_last_increment` smallint(6) NOT NULL default '0',
`quotation_last_increment` smallint(6) NOT NULL default '0',
`asset_last_increment` smallint(6) NOT NULL default '0',
`payplug_parameters` varchar(255) collate utf8_unicode_ci default NULL,
`displayed_first_connection_dialog` tinyint(1) NOT NULL default '0',
`displayed_first_invoice_display_dialog` tinyint(1) NOT NULL default '0',
`payplug_parameters_created_at` date default NULL,
`payplug_first_payment_at` date default NULL,
`latest_payplug_http_code` int(11) default NULL,
`register_number` varchar(255) collate utf8_unicode_ci default NULL,
`register_code` varchar(255) collate utf8_unicode_ci default NULL,
`register_bis_city` varchar(255) collate utf8_unicode_ci default NULL,
`register_bis_number` varchar(255) collate utf8_unicode_ci default NULL,
`registerBis_id` int(11) default NULL,
`main_activity_type_id` int(11) default NULL,
`secondary_activity_type_id` int(11) default NULL,
`declaration_reminder_popup` tinyint(1) default NULL,
`declaration_reminder_popup_latest_choice` smallint(6) default NULL COMMENT '1 = Me le rappeler demain, 2 = Ne plus afficher cette alerte',
`declaration_reminder_popup_latest_choice_date` date default NULL,
`main_activity_id` int(11) default NULL,
`secondary_activity_id` int(11) default NULL,
`primary_socio_economic_classification_id` int(11) default NULL,
`secondary_socio_economic_classification_id` int(11) default NULL,
`income_bracket_id` int(11) default NULL,
`main_activity_custom` varchar(255) collate utf8_unicode_ci default NULL,
`secondary_activity_custom` varchar(255) collate utf8_unicode_ci default NULL,
`default_further_information` longtext collate utf8_unicode_ci,
`gclid` varchar(255) collate utf8_unicode_ci default NULL,
`main_activity_type_old_id` smallint(6) default NULL,
`main_activity_nature_old_id` smallint(6) default NULL,
`secondary_activity_type_old_id` smallint(6) default NULL,
`secondary_activity_nature_old_id` smallint(6) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_E904BFD1A76ED395` (`user_id`),
UNIQUE KEY `UNIQ_E904BFD1D1E63CD1` (`insurance_id`),
KEY `IDX_E904BFD14976CB7E` (`register_id`),
KEY `IDX_E904BFD1DBC024CC` (`registerBis_id`),
KEY `IDX_E904BFD12E864BE8` (`main_activity_type_id`),
KEY `IDX_E904BFD132198C62` (`secondary_activity_type_id`),
KEY `IDX_E904BFD15543A800` (`main_activity_id`),
KEY `IDX_E904BFD1798B8812` (`secondary_activity_id`),
KEY `IDX_E904BFD1D17B29D3` (`primary_socio_economic_classification_id`),
KEY `IDX_E904BFD17758CEBC` (`secondary_socio_economic_classification_id`),
KEY `IDX_E904BFD1BAF920D3` (`income_bracket_id`)
) ENGINE=MyISAM AUTO_INCREMENT=116384 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
It seems like this simple amendment will be faster:
SELECT COUNT(DISTINCT a0_.id) sclr0
FROM account a0_
JOIN customer c1_
ON c1_.account = a0_.id
JOIN address a3_
ON c1_.customer_address = a3_.id
And if data integrity is important to you, consider switching to InnoDB.
Change LEFT JOIN address a3 to plain JOIN. That way, the optimizer could consider starting with address. And add INDEX(city) to address.
Currently, the EXPLAIN is showing hitting 405K rows in each of 3 tables. (1.2M total) With the above change, the optimizer would start with the city index, there by looking at, say 1K rows. After that, it would look up 1K rows in each of the other two tables. (3K total)
LEFT JOIN sf_user_data s2_ ON (s2_.user_id = a0_.id) seems to be totally useless; remove it. (Now down to 2K rows)
Don't blindly use (255), use reasonable numbers. In doing so, if identifier is not very big, make it the PRIMARY KEY and get rid of id.
It is extremely rare to have 6 different columns each being UNIQUE. I think you will run into business-logic trouble because of it.

SQL Error After Table Import/Export

Hey I am trying to export the product tables from one opencart store to another,but I have had some issues doing that.
The majority of tables had 0 problems to import/export,however the product table gives the following error.
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 37
Here is the code itself.
CREATE TABLE `product` (
`product_id` int(11) NOT NULL,
`model` varchar(64) NOT NULL,
`sku` varchar(64) NOT NULL,
`upc` varchar(12) NOT NULL,
`ean` varchar(14) NOT NULL,
`jan` varchar(13) NOT NULL,
`isbn` varchar(13) NOT NULL,
`mpn` varchar(64) NOT NULL,
`location` varchar(128) NOT NULL,
`quantity` int(4) NOT NULL,
`stock_status_id` int(11) NOT NULL,
`image` varchar(255) DEFAULT NULL,
`manufacturer_id` int(11) NOT NULL,
`shipping` tinyint(1) NOT NULL DEFAULT '1',
`price` decimal(15,4) NOT NULL DEFAULT '0.0000',
`points` int(8) NOT NULL,
`tax_class_id` int(11) NOT NULL,
`date_available` date NOT NULL,
`weight` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`weight_class_id` int(11) NOT NULL,
`length` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`width` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`height` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`length_class_id` int(11) NOT NULL,
`subtract` tinyint(1) NOT NULL DEFAULT '1',
`minimum` int(11) NOT NULL DEFAULT '1',
`sort_order` int(11) NOT NULL,
`status` tinyint(1) NOT NULL,
`date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`date_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`viewed` int(5) NOT NULL,
`measurement_class_id` int(11) NOT NULL DEFAULT '0',
`maximum` int(11) NOT NULL DEFAULT '0',
`cost` decimal(15,4) NOT NULL DEFAULT '0.0000',
`style` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '0'COMMENT
) ;
Hi you are getting error because you have not completed last line comment code for "style" column do one of the below options:
a) Add comment description
`style` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '0' COMMENT "comment description"
b)remove comment description
`style` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '0'
remove the COMMENT at the last line:
CREATE TABLE `product` (
`product_id` int(11) NOT NULL,
`model` varchar(64) NOT NULL,
`sku` varchar(64) NOT NULL,
`upc` varchar(12) NOT NULL,
`ean` varchar(14) NOT NULL,
`jan` varchar(13) NOT NULL,
`isbn` varchar(13) NOT NULL,
`mpn` varchar(64) NOT NULL,
`location` varchar(128) NOT NULL,
`quantity` int(4) NOT NULL,
`stock_status_id` int(11) NOT NULL,
`image` varchar(255) DEFAULT NULL,
`manufacturer_id` int(11) NOT NULL,
`shipping` tinyint(1) NOT NULL DEFAULT '1',
`price` decimal(15,4) NOT NULL DEFAULT '0.0000',
`points` int(8) NOT NULL,
`tax_class_id` int(11) NOT NULL,
`date_available` date NOT NULL,
`weight` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`weight_class_id` int(11) NOT NULL,
`length` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`width` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`height` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`length_class_id` int(11) NOT NULL,
`subtract` tinyint(1) NOT NULL DEFAULT '1',
`minimum` int(11) NOT NULL DEFAULT '1',
`sort_order` int(11) NOT NULL,
`status` tinyint(1) NOT NULL,
`date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`date_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`viewed` int(5) NOT NULL,
`measurement_class_id` int(11) NOT NULL DEFAULT '0',
`maximum` int(11) NOT NULL DEFAULT '0',
`cost` decimal(15,4) NOT NULL DEFAULT '0.0000',
`style` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '0'
) ;
sample
MariaDB []> CREATE TABLE `product` ( `product_id` int(11) NOT NULL, `model` varchar(64) NOT NULL, `sku` varchar(64) NOT NULL, `upc` varchar(12) NOT NULL, `ean` varchar(14) NOT NULL, `jan` varchar(13) NOT NULL, `isbn` varchar(13) NOT NULL, `mpn` varchar(64) NOT NULL, `location` varchar(128) NOT NULL, `quantity` int(4) NOT NULL, `stock_status_id` int(11) NOT NULL, `image` varchar(255) DEFAULT NULL, `manufacturer_id` int(11) NOT NULL, `shipping` tinyint(1) NOT NULL DEFAULT '1', `price` decimal(15,4) NOT NULL DEFAULT '0.0000', `points` int(8) NOT NULL, `tax_class_id` int(11) NOT NULL, `date_available` date NOT NULL, `weight` decimal(15,8) NOT NULL DEFAULT '0.00000000', `weight_class_id` int(11) NOT NULL, `length` decimal(15,8) NOT NULL DEFAULT '0.00000000', `width` decimal(15,8) NOT NULL DEFAULT '0.00000000', `height` decimal(15,8) NOT NULL DEFAULT '0.00000000', `length_class_id` int(11) NOT NULL, `subtract` tinyint(1) NOT NULL DEFAULT '1', `minimum` int(11) NOT NULL DEFAULT '1', `sort_order` int(11) NOT NULL, `status` tinyint(1) NOT NULL, `date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `date_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `viewed` int(5) NOT NULL, `measurement_class_id` int(11) NOT NULL DEFAULT '0', `maximum` int(11) NOT NULL DEFAULT '0', `cost` decimal(15,4) NOT NULL DEFAULT '0.0000', `style` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '0' );
Query OK, 0 rows affected (0.32 sec)
MariaDB []> select version();
+---------------------+
| version() |
+---------------------+
| 10.1.10-MariaDB-log |
+---------------------+
1 row in set (0.00 sec)
MariaDB []>

InnoDB, how to detect the index types?

I have a table:
CREATE TABLE `posts` (
`post_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`topic_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
`forum_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
`poster_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
`icon_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
`poster_ip` varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '',
`post_time` int(11) unsigned NOT NULL DEFAULT '0',
`post_approved` tinyint(1) unsigned NOT NULL DEFAULT '1',
`post_reported` tinyint(1) unsigned NOT NULL DEFAULT '0',
`enable_bbcode` tinyint(1) unsigned NOT NULL DEFAULT '1',
`enable_smilies` tinyint(1) unsigned NOT NULL DEFAULT '1',
`enable_magic_url` tinyint(1) unsigned NOT NULL DEFAULT '1',
`enable_sig` tinyint(1) unsigned NOT NULL DEFAULT '1',
`post_username` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
`post_subject` blob,
`post_text` mediumtext COLLATE utf8_bin NOT NULL,
`post_checksum` varchar(32) COLLATE utf8_bin NOT NULL DEFAULT '',
`post_attachment` tinyint(1) unsigned NOT NULL DEFAULT '0',
`bbcode_bitfield` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
`bbcode_uid` varchar(8) COLLATE utf8_bin NOT NULL DEFAULT '',
`post_postcount` tinyint(1) unsigned NOT NULL DEFAULT '1',
`post_edit_time` int(11) unsigned NOT NULL DEFAULT '0',
`post_edit_reason` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
`post_edit_user` mediumint(8) unsigned NOT NULL DEFAULT '0',
`post_edit_count` smallint(4) unsigned NOT NULL DEFAULT '0',
`post_edit_locked` tinyint(1) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`post_id`),
KEY `forum_id` (`forum_id`),
KEY `topic_id` (`topic_id`),
KEY `poster_ip` (`poster_ip`),
KEY `poster_id` (`poster_id`),
KEY `post_approved` (`post_approved`),
KEY `post_username` (`post_username`),
KEY `tid_post_time` (`topic_id`,`post_time`) USING HASH
) ENGINE=InnoDB AUTO_INCREMENT=22381 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
as you can see, the last index is "USING HASH" - but when I use show indexes from posts;, thats not showing it
After a quick Google search, I quickly found that InnoDB doesn't support HASH indexes, but still accepts the modifier in table definitions.
SHOW CREATE TABLE may show USING HASH, but SHOW INDEXES will actually show you what index type is being used:
Index_type
The index method used (BTREE, FULLTEXT, HASH, RTREE).

MySQL Can't create table [errno: 150]

Getting the above error on the following script:
DROP TABLE IF EXISTS `incident`;
SET #saved_cs_client = ##character_set_client;
SET character_set_client = utf8;
CREATE TABLE `incident` (
`incident_id` int(11) NOT NULL auto_increment,
`patient_id` int(11) default NULL,
`history_id` int(11) default NULL,
`incident_date` datetime default NULL,
`incidentname_id` int(11) default NULL,
`incident_type_id` int(11) default NULL,
`pop_first_symptom` int(11) default NULL,
`first_symptom_date` datetime default NULL,
`similar_symptom_date` datetime default NULL,
`first_consultation_date` datetime default NULL,
`accident` int(1) default NULL,
`accident_date` datetime default NULL,
`pop_accident_state` int(11) default NULL,
`pop_condition_related_to` int(11) default NULL,
`flag_prothesis` char(1) collate utf8_unicode_ci NOT NULL default '0',
`flag_initial_placement` char(1) collate utf8_unicode_ci NOT NULL default '0',
`flag_orthodontics` char(1) collate utf8_unicode_ci NOT NULL default '0',
`flag_occupational_illness` char(1) collate utf8_unicode_ci NOT NULL default '0',
`box19` varchar(80) collate utf8_unicode_ci NOT NULL default '',
`color` varchar(8) collate utf8_unicode_ci NOT NULL default 'ffffff00',
`referrals_id` int(11) default NULL,
`referral_date` datetime default NULL,
`facility_id` int(11) default NULL,
`attorney_id` int(11) default NULL,
`attorney_notes` text collate utf8_unicode_ci,
`attorney_report_sent` date default NULL,
`attorney_deposition_date` date default NULL,
`attorney_trial_date` date default NULL,
`attorney_notice_filing` char(1) collate utf8_unicode_ci default NULL,
`attorney_final_settlement` date default NULL,
`effective_date` datetime default NULL,
`termination_date` datetime default NULL,
`date` datetime default NULL,
`pop_branch` int(11) default NULL,
`pop_status` int(1) default NULL,
`pop_eligibility` int(1) default NULL,
`partial_begin_date` datetime default NULL,
`partial_end_date` datetime default NULL,
`last_work_date` datetime default NULL,
`partial_return_to_work_date` datetime default NULL,
`total_begin_date` datetime default NULL,
`total_end_date` datetime default NULL,
`last_xray_date` datetime default NULL,
`last_seen_date` date default NULL,
`assumed_date` datetime default NULL,
`relinquished_date` datetime default NULL,
`radiographs` varchar(30) collate utf8_unicode_ci NOT NULL default '',
`oral_images` varchar(30) collate utf8_unicode_ci NOT NULL default '',
`models` varchar(30) collate utf8_unicode_ci NOT NULL default '',
`date_appliance_placed` date default NULL,
`date_prior_placement` date default NULL,
`paymentSourceCode` varchar(10) collate utf8_unicode_ci NOT NULL default '',
`locatorCode` varchar(10) collate utf8_unicode_ci NOT NULL default '',
`SAExceptionCode` varchar(10) collate utf8_unicode_ci NOT NULL default '',
`flag_expanded` int(1) default NULL,
`flag_archived` int(1) default NULL,
`cda_reason_code` int(11) default NULL,
`is_extraction` int(11) default '0',
`extraction_tooth` varchar(10) collate utf8_unicode_ci default NULL,
`initial_placement_date` date default NULL,
`initial_placement_location` int(11) default '0',
`prosthesis_material` int(11) default '0',
PRIMARY KEY (`incident_id`),
KEY `patient_id_key` (`patient_id`),
CONSTRAINT `incident_ibfk_1` FOREIGN KEY (`patient_id`) REFERENCES `patient` (`patient_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9854 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SET character_set_client = #saved_cs_client;
Here is the patient table giving me the same error:
DROP TABLE IF EXISTS `patient`;
SET #saved_cs_client = ##character_set_client;
SET character_set_client = utf8;
CREATE TABLE `patient` (
`patient_id` int(11) NOT NULL auto_increment,
`history_id` int(11) default NULL,
`person_id` int(11) default NULL,
`account_id` int(11) default NULL,
`display_id` varchar(10) collate utf8_unicode_ci default NULL,
`chart_num` varchar(10) collate utf8_unicode_ci default NULL,
`bridge_num` varchar(10) collate utf8_unicode_ci default NULL,
`provider_id` int(11) default NULL,
`office_id` int(11) default NULL,
`feeschedule_id` int(11) default NULL,
`pop_marital_status` int(11) default NULL,
`pop_employment` int(11) default NULL,
`new_patient_date` date default NULL,
`primary_ins_id` int(11) default NULL,
`primary_ins_type` char(1) collate utf8_unicode_ci default NULL,
`insured_id_number` varchar(30) collate utf8_unicode_ci default NULL,
`flag_special_insurance` char(1) collate utf8_unicode_ci default '0',
`flag_family_planning` char(1) collate utf8_unicode_ci default '0',
`flag_epstd` char(1) collate utf8_unicode_ci default '0',
`pop_relation_to_primary` int(11) default NULL,
`pop_relation_to_secondary` int(11) default NULL,
`pop_hipaa_release` int(11) default NULL,
`hipaa_release_date` date default NULL,
`patientNotes` text collate utf8_unicode_ci,
`autoremind_notification_type_id` int(11) NOT NULL default '0',
`ethnicity_id` int(11) default NULL,
`patient_status` int(1) NOT NULL default '0',
`created_by_module` char(1) collate utf8_unicode_ci NOT NULL default 'P',
`pop_patient_exception_code` int(11) default '0',
`pop_native_language` int(11) default '0',
`school_name` varchar(30) collate utf8_unicode_ci default NULL,
`graduation_date` date default NULL,
`hygienist_id` int(11) default NULL,
`school_address_id` int(11) default NULL,
`language_id` int(11) default NULL,
PRIMARY KEY (`patient_id`),
KEY `account_id_key` (`account_id`),
KEY `person_id_key` (`person_id`),
KEY `patient_status_index` (`patient_status`),
KEY `person_id_index` (`person_id`),
KEY `account_id_index` (`account_id`),
CONSTRAINT `patient_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `account` (`account_id`),
CONSTRAINT `patient_ibfk_2` FOREIGN KEY (`person_id`) REFERENCES `person` (`person_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9855 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SET character_set_client = #saved_cs_client;
I assume that your foreign key patient_id is not NULL. It must match to your referenced data type.
`patient_id` int(11) default NULL

mysql query times out after certain periodof inactivity - query cache issue i think

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.