2009-06-10 171 views
0

我有表名爲聯繫人,有近120萬條記錄我們使用 MyIsam引擎,每當我們查詢這個表時,mysql掛起,所以現在我們試着用Innodb引擎來引導我們,如果它放慢了速度,不會掛斷他人mysql性能問題

所以我們希望與MyISAM數據,我們試圖在這個表中的許多指標做出快速,但它的股價下跌,並掛起系統

應該怎樣做,使之更加快速,它不應該掛up系統

這是表格:

CREATE TABLE `contacts` (
    `id` varchar(36) NOT NULL, 
    `deleted` tinyint(1) NOT NULL default '0', 
    `date_entered` datetime NOT NULL default '0000-00-00 00:00:00', 
    `date_modified` datetime NOT NULL default '0000-00-00 00:00:00', 
    `modified_user_id` varchar(36) default NULL, 
    `assigned_user_id` varchar(36) default NULL, 
    `created_by` varchar(36) default NULL, 
    `team_id` varchar(36) default NULL, 
    `salutation` varchar(5) default NULL, 
    `first_name` varchar(100) default '', 
    `last_name` varchar(100) default '', 
    `username` varchar(25) default '', 
    `lead_source` varchar(100) default NULL, 
    `title` varchar(50) default NULL, 
    `department` varchar(100) default NULL, 
    `reports_to_id` varchar(36) default NULL, 
    `birthdate` date default NULL, 
    `do_not_call` char(3) default '0', 
    `phone_home` varchar(25) default NULL, 
    `phone_mobile` varchar(25) default NULL, 
    `phone_work` varchar(25) default '', 
    `phone_other` varchar(25) default NULL, 
    `phone_fax` varchar(25) default '', 
    `email1` varchar(100) default '', 
    `email2` varchar(100) default NULL, 
    `assistant` varchar(75) default NULL, 
    `assistant_phone` varchar(25) default NULL, 
    `email_opt_out` char(3) default 'off', 
    `primary_address_street` varchar(150) default NULL, 
    `primary_address_city` varchar(100) default NULL, 
    `primary_address_state` varchar(100) default NULL, 
    `primary_address_postalcode` varchar(20) default NULL, 
    `primary_address_country` varchar(100) default NULL, 
    `alt_address_street` varchar(150) default NULL, 
    `alt_address_city` varchar(100) default NULL, 
    `alt_address_state` varchar(100) default NULL, 
    `alt_address_postalcode` varchar(20) default NULL, 
    `alt_address_country` varchar(100) default NULL, 
    `description` text, 
    `portal_name` varchar(255) default NULL, 
    `portal_active` tinyint(1) NOT NULL default '0', 
    `portal_app` varchar(255) default NULL, 
    `salesforceid` varchar(36) default NULL, 
    `phone_direct` varchar(25) default NULL, 
    `invalid_email` tinyint(1) default '0', 
    `parent_is_lead` char(3) default 'no', 
    `advisory_board_member` varchar(25) default NULL, 
    `direct_marketing` varchar(25) default NULL, 
    `efx_id` varchar(36) default NULL, 
    `fax_opt_out` char(3) default 'off', 
    `ppc_keyword` varchar(50) default NULL, 
    `status` varchar(25) default NULL, 
    `web_form` varchar(50) default NULL, 
    `efx_export_date` datetime default NULL, 
    `bmtn` varchar(225) default '', 
    `employee_location` varchar(50) default NULL, 
    `pronunciation` varchar(250) default NULL, 
    `duplicate_of` varchar(36) default NULL, 
    `job_category` varchar(50) default NULL, 
    `last_ska_upload_key` varchar(50) default NULL, 
    `persid` varchar(36) default NULL, 
    `last_web_upload_key` varchar(50) default NULL, 
    `last_webinar_upload_key` varchar(50) default NULL, 
    `primary_address_latitude` float default NULL, 
    `primary_address_longitude` float default NULL, 
    `first_name_soundex` varchar(30) default NULL, 
    `last_name_soundex` varchar(30) default NULL, 
    `primary_address_street_soundex` varchar(30) default NULL, 
    `campaign_id` varchar(36) default NULL, 
    `portal_password` varchar(32) default NULL, 
    `pss_branch` varchar(40) default NULL, 
    `pss_id` int(12) default NULL, 
    `source_detail` varchar(100) default NULL, 
    `source` varchar(100) default NULL, 
    `pss_region` varchar(30) default NULL, 
    `source_added` datetime default NULL, 
    `terminated_user` char(3) default 'off', 
    `invite_opt_out` char(3) default 'off', 
    `newsletter_opt_out` char(3) default 'off', 
    `stream_opt_out` char(3) default 'off', 
    PRIMARY KEY (`id`), 
    KEY `idx_contacts_del_last` (`deleted`,`last_name`), 
    KEY `idx_cont_del_reports` (`deleted`,`reports_to_id`,`last_name`), 
    KEY `idx_contact_del_team` (`deleted`,`team_id`), 
    KEY `idx_contact_salesforceid` (`salesforceid`), 
    KEY `idx_contacts_username` (`username`), 
    KEY `idx_email_opt_out` (`email_opt_out`), 
    KEY `idx_primary_address_street` (`primary_address_street`), 
    KEY `idx_primary_address_city` (`primary_address_city`), 
    KEY `idx_primary_address_state` (`primary_address_state`), 
    KEY `idx_primary_address_postalcode` (`primary_address_postalcode`), 
    KEY `idx_primary_address_country` (`primary_address_country`), 
    KEY `idx_modified_user_id` (`modified_user_id`), 
    KEY `idx_assigned_user_id` (`assigned_user_id`), 
    KEY `idx_created_by` (`created_by`), 
    KEY `idx_team_id` (`team_id`), 
    KEY `idx_reports_to_id` (`reports_to_id`), 
    KEY `idx_contacts_efx_id` (`efx_id`), 
    KEY `idx_contacts_title1` (`title`,`deleted`), 
    KEY `idx_contacts_email1` (`email1`), 
    KEY `idx_contacts_email2` (`email2`), 
    KEY `idx_contacts_job_category` (`job_category`), 
    KEY `idx_contacts_first_name_sdx` (`first_name_soundex`), 
    KEY `idx_contacts_primary_street_sdx` (`primary_address_street_soundex`), 
    KEY `idx_contacts_last_name_sdx` (`last_name_soundex`), 
    KEY `idx_contacts_portal_name` (`portal_name`), 
    KEY `idx_contacts_portal_active` (`portal_active`), 
    KEY `idx_contacts_del_last_first` (`deleted`,`last_name`,`first_name`), 
    KEY `idx_contacts_del_first` (`deleted`,`first_name`), 
    KEY `idx_pss_id` (`pss_id`), 
    KEY `idx_phone_work_last_name_first_name_deleted` (`phone_work`,`last_name`,`first_name`,`deleted`), 
    KEY `idx_phone_work_last_name_first_name_deleted_sdx` (`phone_work`,`last_name_soundex`,`first_name_soundex`,`deleted`), 
    KEY `idx_email1_last_name_first_name_deleted` (`email1`,`last_name`,`first_name`,`deleted`), 
    KEY `idx_email1_last_name_first_name_deleted_sdx` (`email1`,`last_name_soundex`,`first_name_soundex`,`deleted`), 
    KEY `idx_phone_fax_last_name_first_name_deleted` (`phone_fax`,`last_name`,`first_name`,`deleted`), 
    KEY `idx_phone_fax_last_name_first_name_deleted_sdx` (`phone_fax`,`last_name_soundex`,`first_name_soundex`,`deleted`), 
    KEY `idx_phone_work_last_name_deleted` (`phone_work`,`last_name`,`deleted`), 
    KEY `idx_phone_work_last_name_deleted_sdx` (`phone_work`,`last_name_soundex`,`deleted`), 
    KEY `idx_email1_last_name_deleted` (`email1`,`last_name`,`deleted`), 
    KEY `idx_email1_last_name_deleted_sdx` (`email1`,`last_name_soundex`,`deleted`), 
    KEY `idx_phone_fax_last_name_deleted` (`phone_fax`,`last_name`,`deleted`), 
    KEY `idx_phone_fax_last_name_deleted_sdx` (`phone_fax`,`last_name_soundex`,`deleted`), 
    KEY `idx_email1_first_name_deleted` (`email1`,`first_name`,`deleted`), 
    KEY `idx_email1_first_name_deleted_sdx` (`email1`,`first_name_soundex`,`deleted`), 
    KEY `idx_phone_fax_first_name_deleted` (`phone_fax`,`first_name`,`deleted`), 
    KEY `idx_phone_fax_first_name_deleted_sdx` (`phone_fax`,`first_name_soundex`,`deleted`), 
    KEY `idx_email1_deleted` (`email1`,`deleted`), 
    KEY `idx_last_name_first_name_deleted_sdx` (`last_name_soundex`,`first_name_soundex`,`deleted`), 
    KEY `idx_phone_mobile_deleted` (`phone_mobile`,`deleted`,`id`), 
    KEY `idx_first_name_bmtn` (`first_name`,`bmtn`), 
    KEY `idx_first_name_bmtn_email1` (`first_name`,`bmtn`,`email1`), 
    KEY `idx_bmtn_email1` (`bmtn`,`email1`), 
    KEY `idx_deleted` (`deleted`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

-

SELECT acc.id, acc.name, con_reports_to.first_name, con_reports_to.last_name 
from contacts 
left join accounts_contacts a_c on a_c.contact_id = '9802f40d-78bb-8dd4-dfaa-43f1064ccd5e' and a_c.deleted=0 
left join accounts acc on a_c.account_id = acc.id and acc.deleted=0 
left join contacts con_reports_to on con_reports_to.id = contacts.reports_to_id 
where contacts.id = '9802f40d-78bb-8dd4-dfaa-43f1064ccd5e' 
+0

編寫高效的查詢並確定正確的索引? – 2009-06-10 03:16:17

+0

你能描述一下你的硬件設置嗎... – 2009-06-10 03:22:49

+0

linux suse server box有4GB ram 60 gb分區for DB mysql 5.1 and apache 1.33 – maxjackie 2009-06-10 03:50:16

回答

1

我懷疑斷言「每當我們查詢這個表時,mysql掛起」就是一個高價 - 例如,用MyISAM,SELECT COUNT(*) FROM TheTable應該是很快快,本質上是「不管是什麼」。當然,一些查詢將會很慢 - 特別是如果該表未針對查詢進行正確索引,或者MySQL的所謂優化器正在挑選錯誤的策略(但您可以給出提示)。

你爲什麼不向我們展示CREATE TABLE(包括指數),一對夫婦花費太長時間的查詢,他們需要多長時間和非常精確的測量,EXPLAIN SELECT(& C)的輸出那些情侶查詢 - 我敢打賭,我們可以真的然後有一些幫助!

編輯:CREATE TABLE實際上表明該表過於「寬泛」 - 太多列 - 以期望體面的表現(即使沒有顯示查詢)。該模式需要重新設計,將這個巨大的單一表格(例如地址相關信息)的塊拆分成其他輔助表格。究竟如何做到最好,完全取決於對優化最重要的查詢,因此,不知道有問題的查詢,我甚至不會去嘗試這個任務。

再次編輯:所以查詢已發佈並使用其他表,accountsaccount_contacts,以及描述的非常廣泛的contacts;張貼(試圖通過格式化& C到它的意義)的查詢是:

SELECT acc.id, acc.name, con_reports_to.first_name, con_reports_to.last_name 
FROM contacts 
LEFT JOIN accounts_contacts a_c 
      ON a_c.contact_id = '9802f40d-78bb-8dd4-dfaa-43f1064ccd5e' AND 
      a_c.deleted=0 
LEFT JOIN accounts acc 
      ON a_c.account_id = acc.id AND 
      acc.deleted=0 
LEFT JOIN contacts con_reports_to 
      ON con_reports_to.id = contacts.reports_to_id 
WHERE contacts.id = '9802f40d-78bb-8dd4-dfaa-43f1064ccd5e' 

爲什麼LEFT JOIN這兒,而不是正常的INNER加入?在每種情況下是否可能在右側的桌子上沒有相應的行?例如,如果有一個在a_c使用給定值沒有線contact_iddeleted,然後在第一LEFT JOINa_c所有字段將是NULL,所以不可能有對應於acc任一:是重要的發射NULL, NULL作爲前兩列在這種情況下?此外,條件a_cacc根本沒有提及contacts,所以這將是笛卡爾積:從acc中選擇的每一行(如果有的話)將與從con_reports_to中選擇的每一行配對。因此,a_c/acc查詢可能完全與contactscon_reports之間的查詢分離,可能會大大簡化查詢(兩個邏輯上分開的結果當然可以輕鬆地再次放在客戶端中)。

EXPLAIN SELECT對這個複雜的查詢有什麼意義?對於我建議的兩個較輕的單獨的查詢,它有什麼用? accountsaccount_contact表中有哪些索引?

0

水平分割?儘管我猜想有120萬條記錄沒有那麼多來引入水平分割..試圖找到底部的脖子...也可能存在問題,你的硬件以及例如硬盤幾乎已滿等。