要调整哪些设置以使用大量列和一些JOINS改进此SELECT?

时间:2015-10-27 08:46:25

标签: mysql performance select optimization

我正在进行一些项目的本地Web开发,而我的计算机是运行Windows 7且安装了基本XAMPP的Macbook。

Processor: Intel(R) COre(TM) i52415M CPU @ 2.30GHz
RAM: 4GB (2,16GB usable) 
Type: 32-bit Operating System

我知道这不是一台快速的计算机,但我仍然想知道如何更好地优化MySQL以在合适的时间内返回结果。

所以我有两个基本相同的查询,但是一个选择3个表中的所有列,而第二个只选择每个表中的ID。

包含所有列的慢查询:

SELECT COUNT(*) FROM (
SELECT `demanda`.`id` AS `t0_c0`,
`demanda`.`client_id` AS `t0_c1`, 
`demanda`.`alquilerdesde` AS `t0_c2`, 
`demanda`.`alquilerhasta` AS `t0_c3`, 
`demanda`.`ascensor` AS `t0_c4`, 
`demanda`.`balcon` AS `t0_c5`, 
`demanda`.`ventadesde` AS `t0_c6`, 
`demanda`.`electro` AS `t0_c7`, 
`demanda`.`financiacion` AS `t0_c8`, 
`demanda`.`garaje` AS `t0_c9`, 
`demanda`.`ventahasta` AS `t0_c10`, 
`demanda`.`lista_tipos` AS `t0_c11`, 
`demanda`.`lista_zonas` AS `t0_c12`, 
`demanda`.`plantamax` AS `t0_c13`, 
`demanda`.`banosmin` AS `t0_c14`, 
`demanda`.`plantamin` AS `t0_c15`, 
`demanda`.`aseosmin` AS `t0_c16`, 
`demanda`.`estadomin` AS `t0_c17`, 
`demanda`.`habitacionmin` AS `t0_c18`, 
`demanda`.`metrosmin` AS `t0_c19`, 
`demanda`.`parcelamin` AS `t0_c20`, 
`demanda`.`muebles` AS `t0_c21`, 
`demanda`.`opcioncompra` AS `t0_c22`, 
`demanda`.`patio` AS `t0_c23`, 
`demanda`.`piscina` AS `t0_c24`, 
`demanda`.`primeralinea` AS `t0_c25`, 
`demanda`.`sincomision` AS `t0_c26`, 
`demanda`.`solicitasubvencion` AS `t0_c27`, 
`demanda`.`solopromociones` AS `t0_c28`, 
`demanda`.`terraza` AS `t0_c29`, 
`demanda`.`tipomes` AS `t0_c30`, 
`demanda`.`trastero` AS `t0_c31`, 
`demanda`.`ultimopiso` AS `t0_c32`, 
`demanda`.`urbanizacion` AS `t0_c33`, 
`demanda`.`zonacomunitaria` AS `t0_c34`, 
`demanda`.`description` AS `t0_c35`, 
`demanda`.`createDate` AS `t0_c36`, 
`demanda`.`lastUpdated` AS `t0_c37`, 
`demanda`.`updatedBy` AS `t0_c38`, 
`demanda`.`assignedTo` AS `t0_c39`, 
`demanda`.`oioffice_id` AS `t0_c40`, 
`demanda`.`cruces` AS `t0_c41`, 
`demanda`.`fecha_cruces` AS `t0_c42`, 
`demanda`.`orientacion` AS `t0_c43`, 
`demanda`.`situacion` AS `t0_c44`, 
`demanda`.`contratofijo` AS `t0_c45`, 
`demanda`.`declaracioniva` AS `t0_c46`, 
`demanda`.`extranjero` AS `t0_c47`, 
`demanda`.`prioridad` AS `t0_c48`, 
`demanda`.`pensionista` AS `t0_c49`, 
`demanda`.`reciboautonomo` AS `t0_c50`, 
`demanda`.`vehiculo` AS `t0_c51`, 
`demanda`.`nomina` AS `t0_c52`, 
`demanda`.`numdemanda` AS `t0_c53`, 
`demanda`.`jornadacompleta` AS `t0_c54`, 
`demanda`.`mincomision` AS `t0_c55`, 
`demanda`.`ciudadempresa` AS `t0_c56`, 
`demanda`.`automatico` AS `t0_c57`, 
`demanda`.`adultos` AS `t0_c58`, 
`demanda`.`ninos` AS `t0_c59`, 
`demanda`.`edades` AS `t0_c60`, 
`demanda`.`paracuando` AS `t0_c61`, 
`demanda`.`estancia` AS `t0_c62`, 
`demanda`.`mascotas` AS `t0_c63`, 
`demanda`.`avalista` AS `t0_c64`, 
`demanda`.`mediocontacto` AS `t0_c65`, 
`demanda`.`motivacion` AS `t0_c66`, 
`demanda`.`datosinteres` AS `t0_c67`, 
`demanda`.`profesion` AS `t0_c68`, 
`demanda`.`duracion` AS `t0_c69`, 
`demanda`.`empresa` AS `t0_c70`, 
`demanda`.`destacado` AS `t0_c71`, 
`demanda`.`llamar` AS `t0_c72`, 
`demanda`.`localidad` AS `t0_c73`, 
`demanda`.`smsauto` AS `t0_c74`, 
`demanda`.`titulo` AS `t0_c75`, 
`agent`.`id` AS `t1_c0`, 
`agent`.`firstName` AS `t1_c1`, 
`agent`.`lastName` AS `t1_c2`, 
`agent`.`username` AS `t1_c3`, 
`agent`.`password` AS `t1_c4`, 
`agent`.`title` AS `t1_c5`, 
`agent`.`department` AS `t1_c6`, 
`agent`.`officePhone` AS `t1_c7`, 
`agent`.`cellPhone` AS `t1_c8`, 
`agent`.`homePhone` AS `t1_c9`, 
`agent`.`address` AS `t1_c10`, 
`agent`.`backgroundInfo` AS `t1_c11`, 
`agent`.`emailAddress` AS `t1_c12`, 
`agent`.`status` AS `t1_c13`, `agent`.
`temporary` AS `t1_c14`, 
`agent`.`lastUpdated` AS `t1_c15`, 
`agent`.`updatedBy` AS `t1_c16`, 
`agent`.`recentItems` AS `t1_c17`, 
`agent`.`topContacts` AS `t1_c18`, 
`agent`.`lastLogin` AS `t1_c19`, 
`agent`.`login` AS `t1_c20`, 
`agent`.`showCalendars` AS `t1_c21`, 
`agent`.`calendarViewPermission` AS `t1_c22`, 
`agent`.`calendarEditPermission` AS `t1_c23`, 
`agent`.`calendarFilter` AS `t1_c24`, 
`agent`.`setCalendarPermissions` AS `t1_c25`, 
`agent`.`inviteKey` AS `t1_c26`, 
`agent`.`userKey` AS `t1_c27`, 
`agent`.`role` AS `t1_c28`, 
`agent`.`office_id` AS `t1_c29`, 
`client`.`id` AS `t2_c0`, `client`.`name` AS `t2_c1`, 
`client`.`nameId` AS `t2_c2`, 
`client`.`firstName` AS `t2_c3`, 
`client`.`lastName` AS `t2_c4`, 
`client`.`title` AS `t2_c5`, 
`client`.`company` AS `t2_c6`, 
`client`.`phone` AS `t2_c7`, 
`client`.`phone2` AS `t2_c8`, 
`client`.`email` AS `t2_c9`, 
`client`.`website` AS `t2_c10`, 
`client`.`address` AS `t2_c11`, 
`client`.`address2` AS `t2_c12`, 
`client`.`city` AS `t2_c13`, 
`client`.`state` AS `t2_c14`, 
`client`.`zipcode` AS `t2_c15`, 
`client`.`country` AS `t2_c16`, 
`client`.`visibility` AS `t2_c17`, 
`client`.`assignedTo` AS `t2_c18`, 
`client`.`backgroundInfo` AS `t2_c19`, 
`client`.`twitter` AS `t2_c20`, 
`client`.`linkedin` AS `t2_c21`, 
`client`.`skype` AS `t2_c22`, 
`client`.`googleplus` AS `t2_c23`, 
`client`.`lastUpdated` AS `t2_c24`, 
`client`.`lastActivity` AS `t2_c25`, 
`client`.`updatedBy` AS `t2_c26`, 
`client`.`priority` AS `t2_c27`, 
`client`.`leadSource` AS `t2_c28`, 
`client`.`leadDate` AS `t2_c29`, 
`client`.`rating` AS `t2_c30`, 
`client`.`createDate` AS `t2_c31`, 
`client`.`facebook` AS `t2_c32`, 
`client`.`otherUrl` AS `t2_c33`, 
`client`.`leadtype` AS `t2_c34`, 
`client`.`closedate` AS `t2_c35`, 
`client`.`interest` AS `t2_c36`, 
`client`.`leadstatus` AS `t2_c37`, 
`client`.`dealvalue` AS `t2_c38`, 
`client`.`leadscore` AS `t2_c39`, 
`client`.`dealstatus` AS `t2_c40`, 
`client`.`timezone` AS `t2_c41`, 
`client`.`doNotCall` AS `t2_c42`, 
`client`.`doNotEmail` AS `t2_c43`, 
`client`.`trackingKey` AS `t2_c44`, 
`client`.`dupeCheck` AS `t2_c45`, 
`client`.`c_mobile` AS `t2_c46`, 
`client`.`c_language` AS `t2_c47`, 
`client`.`c_conyuge` AS `t2_c48`, 
`client`.`c_conemail` AS `t2_c49`, 
`client`.`c_telefono4` AS `t2_c50`, 
`client`.`c_telefono5` AS `t2_c51`, 
`client`.`c_nif` AS `t2_c52`, 
`client`.`c_connif` AS `t2_c53` 
FROM `x2_oirequest` `demanda` 
LEFT OUTER JOIN `x2_users` `agent` ON 
    (`demanda`.`assignedTo`=`agent`.`id`) 
LEFT OUTER JOIN `x2_contacts` `client` ON
    (`demanda`.`client_id`=`client`.`id`) 
GROUP BY demanda.id
) sq;

这是一个只有ID的快速:

SELECT COUNT(*) FROM (SELECT `demanda`.id as did, `agent`.`id` as aid, `client`.`id` as cid 
FROM `x2_oirequest` `demanda` 
LEFT OUTER JOIN `x2_users` `agent` ON (`demanda`.`assignedTo`=`agent`.`id`) 
LEFT OUTER JOIN `x2_contacts` `client` ON (`demanda`.`client_id`=`client`.`id`) 
GROUP BY demanda.id) sq;

第一个运行大约需要5-7秒,而第二个运行几乎立即显示结果。

同样在生产服务器上,一个64GB RAM的专用服务器,第一个查询非常快,根本不慢。

所以我的问题是,我应该在MySQL配置中进行本地调整,以便在选择所有这些列时它的工作速度更快?我尝试修改一些缓冲区,从7s到4.5s,但仍然很慢。

我的数据库类型是InnoDB。

由于我已经测试了很多并且没有找到改善时间的方法,我终于激活了query_cache,现在在第一次加载后,它非常快,所以它可以在这种情况下解决我的问题但是我确信这不是最好的解决方案,必须有别的东西。

最后,这是我当前的MySQL配置:

# Example MySQL config file for small systems.
#
# This is for a system with little memory (<= 64M) where MySQL is only used
# from time to time and it's important that the mysqld daemon
# doesn't use much resources.
#
# You can copy this file to
# C:/xampp/mysql/bin/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options (in this
# installation this directory is C:/xampp/mysql/data) or
# ~/.my.cnf to set user-specific options.
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.

# The following options will be passed to all MySQL clients
[client] 
# password       = your_password 
port            = 3306 
socket          = "C:/xampp/mysql/mysql.sock"


# Here follows entries for some specific programs 

# The MySQL server
[mysqld]
port= 3306
socket = "C:/xampp/mysql/mysql.sock"
basedir = "C:/xampp/mysql" 
tmpdir = "C:/xampp/tmp" 
datadir = "C:/xampp/mysql/data"
pid_file = "mysql.pid"
# enable-named-pipe
key_buffer = 16M
max_allowed_packet = 1M
net_buffer_length = 8K
myisam_sort_buffer_size = 8M
log_error = "mysql_error.log"



# Size of the buffer used for doing full table scans.
# Allocated per thread, if a full scan is needed. > 2M
read_buffer_size = 16M

# When reading rows in sorted order after a sort, the rows are read
# through this buffer to avoid disk seeks. You can improve ORDER BY
# performance a lot, if set this to a high value.
# Allocated per thread, when needed. > 16M
read_rnd_buffer_size = 32M

# Sort buffer is used to perform sorts for some ORDER BY and GROUP BY
# queries. If sorted data does not fit into the sort buffer, a disk
# based merge sort is used instead - See the "Sort_merge_passes"
# status variable. Allocated per thread if sort is needed. > 8M
sort_buffer_size = 32M

# This buffer is used for the optimization of full JOINs (JOINs without
# indexes). Such JOINs are very bad for performance in most cases
# anyway, but setting this variable to a large value reduces the
# performance impact. See the "Select_full_join" status variable for a
# count of full JOINs. Allocated per thread if full join is found > 8M
join_buffer_size = 64M


query_cache_size=32M
query_cache_type=1
query_cache_limit=16M




# Change here for bind listening
# bind-address="127.0.0.1" 
# bind-address = ::1          # for ipv6

# Where do all the plugins live
plugin_dir = "C:/xampp/mysql/lib/plugin/" 

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
# 
# commented in by lampp security
#skip-networking
skip-federated

# Replication Master Server (default)
# binary logging is required for replication
# log-bin deactivated by default since XAMPP 1.4.11
#log-bin=mysql-bin

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id   = 1

# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
#    the syntax is:
#
#    CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
#    MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
#
#    where you replace <host>, <user>, <password> by quoted strings and
#    <port> by the master's port number (3306 by default).
#
#    Example:
#
#    CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
#    MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
#    start replication for the first time (even unsuccessfully, for example
#    if you mistyped the password in master-password and the slave fails to
#    connect), the slave will create a master.info file, and any later
#    change in this file to the variables' values below will be ignored and
#    overridden by the content of the master.info file, unless you shutdown
#    the slave server, delete master.info and restart the slaver server.
#    For that reason, you may want to leave the lines below untouched
#    (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id       = 2
#
# The replication master for this slave - required
#master-host     =   <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user     =   <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password =   <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port     =  <port>
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin


# Point the following paths to different dedicated disks
#tmpdir = "C:/xampp/tmp"
#log-update = /path-to-dedicated-directory/hostname

# Uncomment the following if you are using BDB tables
#bdb_cache_size = 4M
#bdb_max_lock = 10000

# Comment the following if you are using InnoDB tables
#skip-innodb
innodb_data_home_dir = "C:/xampp/mysql/data"
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = "C:/xampp/mysql/data"
#innodb_log_arch_dir = "C:/xampp/mysql/data"
## You can set .._buffer_pool_size up to 50 - 80 %
## of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
innodb_buffer_pool_size = 512M
innodb_additional_mem_pool_size = 64M
## Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50

## UTF 8 Settings
#init-connect=\'SET NAMES utf8\'
#collation_server=utf8_unicode_ci
#character_set_server=utf8
#skip-character-set-client-handshake
#character_sets-dir="C:/xampp/mysql/share/charsets"

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[isamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[myisamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

更新:

表中的行:

  • x2_oirequest约10k
  • x2_contacts约10k
  • x2_users很少

表格结构:

CREATE TABLE `x2_contacts` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(255) DEFAULT NULL,
 `nameId` varchar(250) DEFAULT NULL,
 `firstName` varchar(255) NOT NULL,
 `lastName` varchar(255) NOT NULL,
 `title` varchar(100) DEFAULT NULL,
 `company` varchar(250) DEFAULT NULL,
 `phone` varchar(40) DEFAULT NULL,
 `phone2` varchar(40) DEFAULT NULL,
 `email` varchar(250) DEFAULT NULL,
 `website` varchar(250) DEFAULT NULL,
 `address` varchar(250) DEFAULT NULL,
 `address2` varchar(250) DEFAULT NULL,
 `city` varchar(40) DEFAULT NULL,
 `state` varchar(40) DEFAULT NULL,
 `zipcode` varchar(20) DEFAULT NULL,
 `country` varchar(40) DEFAULT NULL,
 `visibility` int(11) NOT NULL,
 `assignedTo` varchar(50) DEFAULT NULL,
 `backgroundInfo` text,
 `twitter` varchar(50) DEFAULT NULL,
 `linkedin` varchar(100) DEFAULT NULL,
 `skype` varchar(32) DEFAULT NULL,
 `googleplus` varchar(100) DEFAULT NULL,
 `lastUpdated` bigint(20) DEFAULT NULL,
 `lastActivity` bigint(20) DEFAULT NULL,
 `updatedBy` varchar(50) DEFAULT NULL,
 `priority` varchar(40) DEFAULT NULL,
 `leadSource` varchar(40) DEFAULT NULL,
 `leadDate` bigint(20) DEFAULT NULL,
 `rating` tinyint(4) DEFAULT NULL,
 `createDate` bigint(20) DEFAULT NULL,
 `facebook` varchar(100) DEFAULT NULL,
 `otherUrl` varchar(100) DEFAULT NULL,
 `leadtype` varchar(250) DEFAULT NULL,
 `closedate` bigint(20) DEFAULT NULL,
 `interest` varchar(250) DEFAULT NULL,
 `leadstatus` varchar(250) DEFAULT NULL,
 `dealvalue` decimal(18,2) DEFAULT NULL,
 `leadscore` int(11) DEFAULT NULL,
 `dealstatus` varchar(250) DEFAULT NULL,
 `timezone` varchar(250) DEFAULT NULL,
 `doNotCall` tinyint(4) DEFAULT '0',
 `doNotEmail` tinyint(4) DEFAULT '0',
 `trackingKey` varchar(32) DEFAULT NULL,
 `dupeCheck` int(11) DEFAULT '0',
 `c_mobile` varchar(255) DEFAULT NULL,
 `c_language` varchar(255) DEFAULT NULL,
 `c_conyuge` varchar(255) DEFAULT NULL,
 `c_conemail` varchar(255) DEFAULT NULL,
 `c_telefono4` varchar(255) DEFAULT NULL,
 `c_telefono5` varchar(255) DEFAULT NULL,
 `c_nif` varchar(255) DEFAULT NULL,
 `c_connif` varchar(255) DEFAULT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `nameId` (`nameId`),
 KEY `email` (`email`),
 KEY `assignedTo` (`assignedTo`),
 KEY `company` (`company`),
 KEY `c_mobile` (`c_mobile`),
 KEY `c_nif` (`c_nif`)
) ENGINE=InnoDB AUTO_INCREMENT=9623 DEFAULT CHARSET=utf8;

CREATE TABLE `x2_oirequest` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `client_id` int(10) unsigned DEFAULT NULL,
 `alquilerdesde` int(11) DEFAULT NULL,
 `alquilerhasta` int(11) DEFAULT NULL,
 `ascensor` tinyint(1) DEFAULT NULL,
 `balcon` tinyint(1) DEFAULT NULL,
 `ventadesde` int(11) DEFAULT NULL,
 `electro` tinyint(1) DEFAULT NULL,
 `financiacion` int(11) DEFAULT NULL,
 `garaje` tinyint(1) DEFAULT NULL,
 `ventahasta` int(11) DEFAULT NULL,
 `lista_tipos` varchar(3000) DEFAULT NULL,
 `lista_zonas` varchar(3000) DEFAULT NULL,
 `plantamax` int(11) DEFAULT NULL,
 `banosmin` int(11) DEFAULT NULL,
 `plantamin` int(11) DEFAULT NULL,
 `aseosmin` int(11) DEFAULT NULL,
 `estadomin` int(11) DEFAULT NULL,
 `habitacionmin` int(11) DEFAULT NULL,
 `metrosmin` int(11) DEFAULT NULL,
 `parcelamin` int(11) DEFAULT NULL,
 `muebles` tinyint(1) DEFAULT NULL,
 `opcioncompra` tinyint(1) DEFAULT NULL,
 `patio` tinyint(1) DEFAULT NULL,
 `piscina` tinyint(1) DEFAULT NULL,
 `primeralinea` tinyint(1) DEFAULT NULL,
 `sincomision` tinyint(1) DEFAULT NULL,
 `solicitasubvencion` tinyint(1) DEFAULT NULL,
 `solopromociones` tinyint(1) DEFAULT NULL,
 `terraza` tinyint(1) DEFAULT NULL,
 `tipomes` int(11) DEFAULT NULL,
 `trastero` tinyint(1) DEFAULT NULL,
 `ultimopiso` tinyint(1) DEFAULT NULL,
 `urbanizacion` tinyint(1) DEFAULT NULL,
 `zonacomunitaria` tinyint(4) DEFAULT NULL,
 `description` text,
 `createDate` int(11) DEFAULT NULL,
 `lastUpdated` int(11) DEFAULT NULL,
 `updatedBy` varchar(250) DEFAULT NULL,
 `assignedTo` int(10) unsigned NOT NULL,
 `oioffice_id` int(11) DEFAULT NULL,
 `cruces` int(10) unsigned DEFAULT '0',
 `fecha_cruces` date DEFAULT NULL,
 `orientacion` int(10) unsigned DEFAULT NULL,
 `situacion` int(11) DEFAULT NULL,
 `contratofijo` tinyint(4) DEFAULT NULL,
 `declaracioniva` tinyint(4) DEFAULT NULL,
 `extranjero` tinyint(4) DEFAULT NULL,
 `prioridad` int(11) DEFAULT NULL,
 `pensionista` tinyint(4) DEFAULT NULL,
 `reciboautonomo` tinyint(4) DEFAULT NULL,
 `vehiculo` tinyint(4) DEFAULT NULL,
 `nomina` tinyint(4) DEFAULT NULL,
 `numdemanda` int(11) DEFAULT NULL,
 `jornadacompleta` tinyint(4) DEFAULT NULL,
 `mincomision` int(11) DEFAULT NULL,
 `ciudadempresa` varchar(50) DEFAULT NULL,
 `automatico` int(11) DEFAULT NULL,
 `adultos` int(11) DEFAULT NULL,
 `ninos` int(11) DEFAULT NULL,
 `edades` varchar(50) DEFAULT NULL,
 `paracuando` varchar(50) DEFAULT NULL,
 `estancia` varchar(50) DEFAULT NULL,
 `mascotas` int(11) DEFAULT NULL,
 `avalista` tinyint(4) DEFAULT NULL,
 `mediocontacto` varchar(50) DEFAULT NULL,
 `motivacion` varchar(500) DEFAULT NULL,
 `datosinteres` text,
 `profesion` varchar(250) DEFAULT NULL,
 `duracion` varchar(250) DEFAULT NULL,
 `empresa` varchar(250) DEFAULT NULL,
 `destacado` varchar(250) DEFAULT NULL,
 `llamar` tinyint(1) DEFAULT '0',
 `localidad` varchar(250) DEFAULT NULL,
 `smsauto` varchar(250) DEFAULT NULL,
 `titulo` varchar(250) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `FK_x2_oidemanda_x2_contacts` (`client_id`),
 KEY `FK_x2_oidemanda_x2_users` (`assignedTo`),
 KEY `index4` (`client_id`),
 KEY `FK_x2_oirequest_x2_oirequest_status` (`situacion`),
 KEY `oioffice_id` (`oioffice_id`),
 KEY `fecha_cruces` (`fecha_cruces`),
 KEY `zonacomunitaria` (`zonacomunitaria`),
 CONSTRAINT `FK_x2_oidemanda_x2_contacts` FOREIGN KEY (`client_id`) REFERENCES `x2_contacts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `FK_x2_oidemanda_x2_users` FOREIGN KEY (`assignedTo`) REFERENCES `x2_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `FK_x2_oirequest_x2_oirequest_status` FOREIGN KEY (`situacion`) REFERENCES `x2_oirequest_status` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=10257 DEFAULT CHARSET=utf8;

CREATE TABLE `x2_users` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `firstName` varchar(100) DEFAULT NULL,
 `lastName` varchar(120) DEFAULT NULL,
 `username` varchar(50) DEFAULT NULL,
 `password` varchar(100) DEFAULT NULL,
 `title` varchar(50) DEFAULT NULL,
 `department` varchar(40) DEFAULT NULL,
 `officePhone` varchar(40) DEFAULT NULL,
 `cellPhone` varchar(40) DEFAULT NULL,
 `homePhone` varchar(40) DEFAULT NULL,
 `address` varchar(100) DEFAULT NULL,
 `backgroundInfo` text,
 `emailAddress` varchar(100) NOT NULL,
 `status` tinyint(4) NOT NULL,
 `temporary` tinyint(4) DEFAULT '0',
 `lastUpdated` varchar(50) DEFAULT NULL,
 `updatedBy` varchar(50) DEFAULT NULL,
 `recentItems` varchar(100) DEFAULT NULL,
 `topContacts` varchar(100) DEFAULT NULL,
 `lastLogin` int(11) DEFAULT '0',
 `login` int(11) DEFAULT '0',
 `showCalendars` text,
 `calendarViewPermission` text,
 `calendarEditPermission` text,
 `calendarFilter` text,
 `setCalendarPermissions` tinyint(4) DEFAULT NULL,
 `inviteKey` varchar(16) DEFAULT NULL,
 `userKey` varchar(32) DEFAULT NULL,
 `role` varchar(64) DEFAULT NULL,
 `office_id` int(11) DEFAULT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `username` (`username`,`emailAddress`),
 KEY `username_2` (`username`),
 KEY `role` (`role`),
 KEY `office_id` (`office_id`),
 CONSTRAINT `x2_users_ibfk_1` FOREIGN KEY (`office_id`) REFERENCES `x2_oioffice` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=77 DEFAULT CHARSET=utf8;

2 个答案:

答案 0 :(得分:1)

杀手是你基本上把整个数据库拉出来......当然这需要时间。缓存一切并不实际......如果你的表增长到500k请求,70k联系人和100个用户,会发生什么。你要缓存整个事情吗?我不知道你预期的增长预期。

此外,您正在抓取160列。对于现实情况来说,这是多么实际。大多数基于Web的查询都基于不是拉动整个数据库的特定标准,而是基于可能包含所有列的较小块。

第一个建议是将一个覆盖GROUP BY和JOIN的索引作为一个复合索引覆盖其他表,否则,引擎必须转到每个页面以限定查询的表连接。无论如何,它正在这样做,因为你抓住所有列,但不是重点。此外,当您开始进入标准时(例如来自特定用户的所有请求,或所有用户的所有特定类型的请求......),请将搜索条件添加为索引中的前导列。

对于我所描述的JOINING基础,我将从查看你的/加入标准开始

   FROM 
      x2_oirequest demanda 
         LEFT OUTER JOIN x2_users agent 
            ON demanda.assignedTo = agent.id  
         LEFT OUTER JOIN x2_contacts client 
            ON demanda.client_id = client.id 
   GROUP BY 
      demanda.id sq;

索引x2_oirequest on(id,assignedTo,client_id)

现在,假设您想要特定用户。我会调整表索引,使其在第一个条件下分配给人。 (assignedTo,id,client_id)。所以当一个

WHERE demanda.AssignedTo = 79

您首先获取所有这些记录(因此被分配到第一个位置),然后,ID就是group by的基础,而client_id仍可用于限定连接。

进一步研究如何构建适当的索引。再次,想想潜在的“我想要获得什么标准”,而不是有多快下拉整个数据库。

答案 1 :(得分:1)

我怀疑你会得到与

相同的答案
SELECT COUNT(*) FROM demanda;

如果是这样,那么LEFT JOINs就无关紧要了。 LEFT说“我不在乎'右'表中是否有一行”。而且,由于您拥有GROUP BY,因此将临时表缩减为每demanda行一行。

另一方面,如果您删除了LEFT和(demandaassignedTo = agentid)指向多个代理的链接,那么你会有效地计算代理人,而不是需求。

结论:请注意COUNT(*)JOINs或其他)的聚合(例如LEFT)。有一个中间结果被夸大(因为JOINs)然后放气(因为GROUP BY)。这通常成本高昂且浪费。