Sql order by - 减少查询执行时间

时间:2012-02-09 12:41:28

标签: mysql sql-order-by

我有一张巨大的桌子,里面有大约6lks的记录和大约10-15列。我将来会分开它。

同时我有一个查询,它从db获取记录,查询如下:

SELECT t1.*
FROM images t1
LEFT JOIN users t4 ON t1.updated_worker_id = t4.id
WHERE .... 
ORDER BY t1.priority, ucase(t1.part) ASC
LIMIT 0,1

部分栏目有索引。

以上查询排序时间太长,我需要的是减少查询执行时间


已更新:

CREATE TABLE `images` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `primary_category_id` int(10) unsigned DEFAULT NULL,
  `secondary_category_id` int(10) unsigned DEFAULT NULL,
  `front_url` varchar(255) DEFAULT NULL,
  `back_url` varchar(255) DEFAULT NULL,
  `title` varchar(100) DEFAULT NULL,
  `part` varchar(10) DEFAULT NULL,
  `photo_id` int(10) unsigned DEFAULT NULL,
  `photo_dt_month` varchar(2) DEFAULT NULL,
  `photo_dt_day` varchar(2) DEFAULT NULL,
  `photo_dt_yr` varchar(4) DEFAULT NULL,
  `type` varchar(25) DEFAULT NULL,
  `size_width` int(10) unsigned DEFAULT NULL,
  `size_height` int(10) unsigned DEFAULT NULL,
  `dpi` int(10) unsigned NOT NULL DEFAULT '0',
  `dpix` int(10) unsigned DEFAULT NULL,
  `dpiy` int(10) unsigned DEFAULT NULL,
  `in_stock` varchar(50) DEFAULT NULL,
  `outlet` varchar(50) DEFAULT NULL,
  `source` varchar(50) DEFAULT NULL,
  `keywords` varchar(255) DEFAULT NULL,
  `emotional_keywords` varchar(255) DEFAULT NULL,
  `mechanical_keywords` varchar(255) DEFAULT NULL,
  `description` text,
  `notes` text,
  `comments` text,
  `exported_to_ebay_dt` datetime DEFAULT NULL,
  `exported_to_ebay` set('Y','N') NOT NULL DEFAULT 'N',
  `updated_worker_id` int(10) unsigned DEFAULT NULL,
  `updated_worker_dt` datetime DEFAULT NULL,
  `locked_worker_id` int(10) unsigned DEFAULT NULL,
  `locked_worker_dt` datetime DEFAULT NULL,
  `updated_admin_id` int(10) unsigned DEFAULT NULL,
  `updated_admin_dt` datetime DEFAULT NULL,
  `added_dt` datetime DEFAULT NULL,
  `updated_manager_id` int(10) unsigned DEFAULT NULL,
  `updated_manager_dt` datetime DEFAULT NULL,
  `manager_review` set('Y','N') NOT NULL DEFAULT 'N',
  `paid_status` set('Y','N') NOT NULL DEFAULT 'N',
  `exported_to_web_dt` datetime DEFAULT NULL,
  `exported_to_web` set('Y','N') DEFAULT 'N',
  `prefix` varchar(50) DEFAULT NULL,
  `is_premium` set('Y','N') DEFAULT 'N',
  `template` varchar(50) DEFAULT 'HIPE_default',
  `photographer` varchar(100) DEFAULT NULL,
  `copyright` varchar(100) DEFAULT NULL,
  `priority` int(4) DEFAULT '1',
  `step` set('1','2') DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `part` (`part`),
  KEY `primary_category_id` (`primary_category_id`),
  KEY `updated_worker_id` (`updated_worker_id`),
  KEY `updated_worker_dt` (`updated_worker_dt`)
) ENGINE=MyISAM AUTO_INCREMENT=1013687 DEFAULT CHARSET=latin1


----------


CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_type` set('admin','worker','employee','manager') DEFAULT NULL,
  `payment_tier_id` int(10) unsigned DEFAULT NULL,
  `fname` varchar(100) DEFAULT NULL,
  `lname` varchar(100) DEFAULT NULL,
  `address1` varchar(255) DEFAULT NULL,
  `address2` varchar(255) DEFAULT NULL,
  `city` varchar(100) DEFAULT NULL,
  `state` varchar(50) DEFAULT NULL,
  `zip` varchar(10) DEFAULT NULL,
  `country` varchar(2) DEFAULT NULL,
  `payment_method` set('paypal','check') NOT NULL DEFAULT 'paypal',
  `paypal_email` varchar(255) DEFAULT NULL,
  `payment_freq` set('monthly','weekly') NOT NULL DEFAULT 'monthly',
  `new_image_notification` set('Y','N') NOT NULL DEFAULT 'N',
  `email` varchar(200) DEFAULT NULL,
  `password` varchar(50) DEFAULT NULL,
  `force_change_pw` enum('Y','N') NOT NULL DEFAULT 'N',
  `in_review` set('Y','N') NOT NULL DEFAULT 'Y',
  `entered_id` int(10) unsigned DEFAULT NULL,
  `entered_dt` datetime DEFAULT NULL,
  `updated_id` int(10) unsigned DEFAULT NULL,
  `updated_dt` datetime DEFAULT NULL,
  `notes` text,
  `need_approval` set('Y','N') NOT NULL DEFAULT 'Y',
  `lock_paypal_email` set('Y','N') NOT NULL DEFAULT 'N',
  `assigned_manager_id` varchar(255) DEFAULT NULL,
  `step` set('1','2') DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM AUTO_INCREMENT=340 DEFAULT CHARSET=latin1

解释结果

id  select_type table   type    possible_keys       key                 key_len ref                                     rows    Extra
1   SIMPLE      t1      ref     updated_worker_id   updated_worker_id   5       const                                   30409   Using where; Using filesort
1   SIMPLE      t4      eq_ref  PRIMARY             PRIMARY             4       worker_admin_new.t1.updated_worker_id   1       Using index

0 个答案:

没有答案