What settings to adjust to improve this SELECT with a lot of columns and a few JOINS?

I'm doing some local web development working on a project and my computer is a Macbook running Windows 7 with a basic XAMPP installation.

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

I know this is not a fast computer, but I still want to know how can I better optimize MySQL to return the results in a decent time.

So I have these two queries, that are basically the same, but one selects all columns from the 3 tables, while the second only selects the ID from each table.

The slow query with all the columns:

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;

And this is the fast one with only IDs:

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;

The first one takes about 5-7seconds to run, while the second one shows the result almost instantly.

Also on the production server, a dedicated server with 64GB RAM, the first query is very fast, not slow at all.

So my question is, what should I tweak locally in MySQL config so it works faster when I select all those columns? I tried to modify some of the buffers and got from 7s to 4.5s but still quite slow.

My database type is InnoDB.

Since I've tested a lot and haven't found a way to improve the time, I have finally activated the query_cache and now after the first load, it is very fast, so it might solve my problem in this case but I'm sure this is not the best solution, there must be something else.

Finally, here is my current MySQL config:

# 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

UPDATE:

Rows in tables:

Table structures:

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;

Upvotes: 0

Views: 76

Answers (2)

Rick James
Rick James

Reputation: 142560

I suspect you will get the same answer with

SELECT COUNT(*) FROM demanda;

If so, then the LEFT JOINs are irrelevant. LEFT says "I don't care if there is a row in the 'right' table". And, since you have the GROUP BY, you shrink the temp table back down to one row per demanda.

If, on the other hand you removed the LEFT, and (demanda.assignedTo = agent.id) links to multiple agents, then you would be effectively counting the agents, not the demandas.

Bottom line: Beware of aggregates (such as COUNT(*)) with JOINs (LEFT or otherwise). There is an intermediate result that is inflated (because of the JOINs) and then deflated (because of the GROUP BY). This is usually costly and wasteful.

Upvotes: 1

DRapp
DRapp

Reputation: 48179

The killer is that you are literally pulling the entire database basically... of course that will take time. Caching it all is not really practical... what happens if your tables grow to 500k requests, 70k contacts and 100s of users. Are you going to cache the entire thing? I don't know your expected growth anticipation.

Also, you are grabbing 160 columns. How practical is that for a reality situation. Most web-based queries are based on specific criteria that ARE NOT pulling the entire database, but smaller chunks that may have all columns.

The first suggestion would be to have an index that covers the GROUP BY and JOINs to other tables as one composite index, otherwise, the engine has to go to each page to qualify the table joins for the query. It is doing that anyhow since you are grabbing all columns though, but not the point. Also, when you do start getting into criteria (such as all requests from a specific user, or all certain types of requests of all users...), add the searching criteria as the leading columns in the index.

For the JOINING basis I described, I would start with looking at your from/join criteria

   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;

Index x2_oirequest on ( id, assignedTo, client_id )

Now, say you want for specific users. I would adjust the tables index to having the assigned to person in the first condition. ( assignedTo, id, client_id ). So when a

WHERE demanda.AssignedTo = 79

you are getting all those records first (hence the assignedTo in the first position), then after that, the ID is the basis of the group by, and the client_id is still available to qualify the join.

Do a little more research on how to build proper indexes. Again, think of the underlying "what CRITERIA do I want to get", not how fast to pull down the entire database.

Upvotes: 1

Related Questions