Whats wrong in my sql query? - mysql

Here I'm trying to create a new table, but I don't know what I did wrong when creating the table.
SQL query:
CREATE DATABASE IF NOT EXISTS wan_ecommerce;
USE wan_ecommerce
CREATE TABLE IF NOT EXISTS users (
user_id int(11) NOT NULL AUTO_INCREMENT COMMENT,
user_name varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT,
user_password_hash varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT,
user_email varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT,
user_active tinyint(1) NOT NULL DEFAULT '0' COMMENT,
user_activation_hash varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT,
user_password_reset_hash char(40) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT,
user_password_reset_timestamp bigint(20) DEFAULT NULL COMMENT,
user_rememberme_token varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT,
user_failed_logins tinyint(1) NOT NULL DEFAULT '0' COMMENT,
user_last_failed_login int(10) DEFAULT NULL COMMENT,
user_registration_datetime datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
user_registration_ip varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0',
PRIMARY KEY (`user_id`),
UNIQUE KEY user_name (`user_name`),
UNIQUE KEY user_email (`user_email`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='user data';
When I run this I get an error. I've added ; after use wan_ecommerce;, but I'm still getting:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near '
user_name varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT,
user_passwo' at line 2
What am I doing wrong here?
How can I solve this?

Use semicolon to terminate each statement.
CREATE DATABASE IF NOT EXISTS wan_ecommerce;
USE wan_ecommerce; -- <----- semi colon was missing
CREATE TABLE IF NOT EXISTS users (
-- ....

The 'COMMENT' is a keyword. If you use it,you must to add a description to flowing 'COMMENT',like this code:
CREATE DATABASE IF NOT EXISTS wan_ecommerce;
USE wan_ecommerce
CREATE TABLE IF NOT EXISTS users (
user_id int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
user_name varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'name',
user_password_hash varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'password_hash',
user_email varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'email',
user_active tinyint(1) NOT NULL DEFAULT '0' COMMENT 'active',
user_activation_hash varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'activation_hash',
user_password_reset_hash char(40) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'password_reset_hash',
user_password_reset_timestamp bigint(20) DEFAULT NULL COMMENT 'password_reset_timestamp',
user_rememberme_token varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'rememberme_token',
user_failed_logins tinyint(1) NOT NULL DEFAULT '0' COMMENT 'failed_logins',
user_last_failed_login int(10) DEFAULT NULL COMMENT 'last_failed_login',
user_registration_datetime datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
user_registration_ip varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0',
PRIMARY KEY (`user_id`),
UNIQUE KEY user_name (`user_name`),
UNIQUE KEY user_email (`user_email`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='user data';
If you don't need this 'COMMENT',you can remove it.The code like this:
CREATE DATABASE IF NOT EXISTS wan_ecommerce;
USE wan_ecommerce
CREATE TABLE IF NOT EXISTS users (
user_id int(11) NOT NULL AUTO_INCREMENT,
user_name varchar(64) COLLATE utf8_unicode_ci NOT NULL,
user_password_hash varchar(255) COLLATE utf8_unicode_ci NOT NULL,
user_email varchar(64) COLLATE utf8_unicode_ci NOT NULL,
user_active tinyint(1) NOT NULL DEFAULT '0',
user_activation_hash varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,
user_password_reset_hash char(40) COLLATE utf8_unicode_ci DEFAULT NULL,
user_password_reset_timestamp bigint(20) DEFAULT NULL,
user_rememberme_token varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
user_failed_logins tinyint(1) NOT NULL DEFAULT '0',
user_last_failed_login int(10) DEFAULT NULL,
user_registration_datetime datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
user_registration_ip varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0',
PRIMARY KEY (`user_id`),
UNIQUE KEY user_name (`user_name`),
UNIQUE KEY user_email (`user_email`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='user data';

Related

mysql #1215 - Cannot add foreign key constraint - but why?

we have the following script:
CREATE TABLE `dataBodyChanges` (
`id` int( 10 ) unsigned NOT NULL AUTO_INCREMENT ,
`numberOfContainers` int( 11 ) NOT NULL ,
`quantityPerContainer` int( 11 ) NOT NULL ,
`grossWeight` double NOT NULL ,
`comment` varchar( 1000 ) NOT NULL ,
`file` int( 10 ) unsigned DEFAULT NULL ,
`user` int( 10 ) unsigned NOT NULL ,
`dataBodyId` int( 10 ) unsigned NOT NULL ,
`created_at` timestamp NULL DEFAULT NULL ,
`updated_at` timestamp NULL DEFAULT NULL ,
PRIMARY KEY ( `id` ) ,
KEY `databodychanges_databodyid_foreign` ( `dataBodyId` ) ,
CONSTRAINT `databodychanges_databodyid_foreign` FOREIGN KEY ( `dataBodyId` ) REFERENCES `dataBodies` ( `id` )
) ENGINE = InnoDB AUTO_INCREMENT =1113 DEFAULT CHARSET = utf8;
with mysql output:
1215 - Cannot add foreign key constraint
the other table is:
USE material;
CREATE TABLE `dataBodies` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`partNumber` varchar(8) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`totalOrderQuantity` varchar(9) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`containerPart` varchar(8) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`numberOfContainers` varchar(9) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`quantityPerContainer` varchar(9) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`kanbanNumber` varchar(4) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`matHandlingCode` varchar(12) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`grossWeight` varchar(9) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`partRemarks` varchar(18) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`AI11Z` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`AI12Z` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`AI13Z` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`AI14Z` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`AI15Z` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`AI16Z` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`AI17Z` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`filler` varchar(163) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`dataHeaderId` int(10) unsigned DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `databodies_dataheaderid_foreign` (`dataHeaderId`),
CONSTRAINT `databodies_dataheaderid_foreign` FOREIGN KEY (`dataHeaderId`) REFERENCES `dataHeaders` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1113 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
We do not know why....
can anybody please help who is master in this?
Thank you in advance!
When you are creating tables, it will check if dependent table is already created, if not it will throw that error.
For which you first need to set,
set foreign_key_checks = false; // above all
Above line will temporarily disable foreign key checks if any.
Then run above query. It will work. then after executing your queries, enable it by,
set foreign_key_checks = true;

Error when importing to server

SQL query:
CREATE TABLE `wp_layerslider` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`author` int(10) NOT NULL DEFAULT '0',
`name` varchar(100) COLLATE utf8mb4_unicode_520_ci DEFAULT '',
`slug` varchar(100) COLLATE utf8mb4_unicode_520_ci DEFAULT '',
`data` mediumtext COLLATE utf8mb4_unicode_520_ci NOT NULL,
`date_c` int(10) NOT NULL,
`date_m` int(10) NOT NULL,
`schedule_start` int(10) NOT NULL DEFAULT '0',
`schedule_end` int(10) NOT NULL DEFAULT '0',
`flag_hidden` tinyint(1) NOT NULL DEFAULT '0',
`flag_deleted` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
MySQL said: Documentation
1273 - Unknown collation: 'utf8mb4_unicode_520_ci'
When attempting to import a database. I'm very new to all of this, hoping someone can assist in simple terms for me
could be you are using an oldeversion of mysql db .. and the collate you need is not avialable .. you could use try alter the table or change the collate
ALTER TABLE `wp_layerslider` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

What could be wrong here

DROP TABLE IF EXISTS 'ci_sessions';
CREATE TABLE 'ci_sessions' (
'session_id' varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '0',
'ip_address' varchar(16) COLLATE utf8_bin NOT NULL DEFAULT '0',
'user_agent' varchar(120) COLLATE utf8_bin DEFAULT NULL,
'last_activity' int(10) unsigned NOT NULL DEFAULT '0',
'user_data' text COLLATE utf8_bin NOT NULL, PRIMARY KEY ('session_id'),
KEY 'last_activity_idx' ('last_activity') )
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
I've tried to run this code on phpMyAdmin and it says
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near ''ci_sessions'' at line 1
It's the same error in all your table and column names. You use ' while you should use backticks:
`
So this should work:
DROP TABLE IF EXISTS `ci_sessions`;
CREATE TABLE `ci_sessions` (
`session_id` varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '0',
`ip_address` varchar(16) COLLATE utf8_bin NOT NULL DEFAULT '0',
`user_agent` varchar(120) COLLATE utf8_bin DEFAULT NULL,
`last_activity` int(10) unsigned NOT NULL DEFAULT '0',
`user_data` text COLLATE utf8_bin NOT NULL, PRIMARY KEY (`session_id`),
KEY `last_activity_idx` (`last_activity`) )
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
As a sidenote - you use NOT NULL and then set the default value to the string 0. quite often. That indicates bad practice, or at least more work for later queries. A column named last_activity for example indicates that null values should be allowed...
Second sidenote - last_activity would typically be a DATETIME column, not an int(10)

Foreign ID in SQL tables to link one table to another

This is my SQL code which links users to items based on tutorials:
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`user_password_hash` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`user_email` varchar(254) COLLATE utf8_unicode_ci NOT NULL,
`user_access_level` tinyint(3) unsigned NOT NULL DEFAULT '0',
`user_active` tinyint(1) NOT NULL DEFAULT '0',
`user_activation_hash` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_password_reset_hash` char(40) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_password_reset_timestamp` bigint(20) DEFAULT NULL,
`user_failed_logins` tinyint(1) NOT NULL DEFAULT '0',
`user_last_failed_login` int(10) DEFAULT NULL,
`user_registration_datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_registration_ip` varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0',
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_name` (`user_name`),
UNIQUE KEY `user_email` (`user_email`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS `item` (
`item_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`item_title` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`item_location` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`item_description` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`item_datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`item_status` int(1) unsigned NOT NULL,
PRIMARY KEY (`item_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
http://sqlfiddle.com/#!9/fd011
I'm getting a bit confused about how to link the items to the user. It seems like I need something called a foreign key on the items, a bit like this in my item table:
FOREIGN KEY (user_id) REFERENCES user(ID)
I can't seem to get it to compile and query successfully. Can anyone please show me the right way to associate the items with the user.
You need to add the user_id column to the items table along with the constraint:
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`user_password_hash` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`user_email` varchar(254) COLLATE utf8_unicode_ci NOT NULL,
`user_access_level` tinyint(3) unsigned NOT NULL DEFAULT '0',
`user_active` tinyint(1) NOT NULL DEFAULT '0',
`user_activation_hash` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_password_reset_hash` char(40) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_password_reset_timestamp` bigint(20) DEFAULT NULL,
`user_failed_logins` tinyint(1) NOT NULL DEFAULT '0',
`user_last_failed_login` int(10) DEFAULT NULL,
`user_registration_datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_registration_ip` varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0',
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_name` (`user_name`),
UNIQUE KEY `user_email` (`user_email`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS `item` (
`item_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
user_id int unsigned,
`item_title` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`item_location` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`item_description` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`item_datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`item_status` int(1) unsigned NOT NULL,
PRIMARY KEY (`item_id`),
FOREIGN KEY (user_id) REFERENCES users(user_id)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
The SQL Fiddle is here.
I should point out a few other things:
Pay attention to the database engine you are using. MyISAM doesn't actually enforce the relationship.
Having weird dates as the default value is probably less useful than just using NULL.
I'm not sure if there is a value to having explicit collations for every character definition, unless your database is going to be supporting a wide variety of collations.
Don't use single quotes for numeric constants. So, if a value is declared as a tinyint, set the default ot 0 not '0' (this doesn't affect performance in a CREATE TABLE statement; it is just misleading).
#GordonLindoff's solution is one method, but that assumes that each item belongs to exactly one user, and an item cannot be referenced by multiple users. If you have a many-to-many relationship, where a user can have multiple items and an item can be referenced by multiple users, then you need a third table that links them together:
CREATE TABLE IF NOT EXISTS `user_item` (
`user_id` int(11) unsigned NOT NULL,
`item_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`user_item`,`item_id`),
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (item_id) REFERENCES items(item_id)
)ENGINE=Innodb DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
The Foreign Key constraints enforce that for every row in user_item that the user_id exists in users, and the item_id exists in items. And as was mentioned in a previous comment, that you will need Innodb to have the foreign key constraints enforced.

MySQL query optimization improvement

Hello all I have a MySQL query that fetches data from more than on tables. Here is my query
SELECT
user_id as id,
user_name as name,
user_phone as phone,
user_email as email,
user_address1 as address1,
user_address2 as address2,
user_city as city,
user_state as state,
user_country as country,
user_available as available,
user_location as location,
user_latitude as latitude,
user_longitude as longitude,
user_description as description,
user_company as company,
user_gender as gender,
(SELECT MIN(service_price) FROM service WHERE service.user_id = a.user_id) as price,
(SELECT service_recomanded FROM service WHERE service.user_id = a.user_id ORDER BY service.service_price ASC LIMIT 1) as recomandad,
verified_email,
verified_facebook,
verified_phone,
verified_twitter,
(SELECT providerphoto_name FROM providerphoto WHERE providerphoto.user_id = a.user_id ORDER BY providerphoto_order ASC LIMIT 1 ) as photo,
(SELECT ROUND( AVG(review_rate),2) FROM review WHERE review.user_id = a.user_id ) AS rate,
(SELECT service_ICOC FROM service WHERE service.user_id = a.user_id ORDER BY service_price ASC LIMIT 1) as type
FROM
user a
WHERE a.user_type = 'provider'
AND a.user_active=1
AND a.user_deleted=0
It gets data from user table, service table, review table and providerphoto table. It works too but the execution time is very slow. I guess to make it a single query and avoid the inner five queries may run it fast. Any help?
Table structures.
--
-- Table structure for table `providerphoto`
--
CREATE TABLE IF NOT EXISTS `providerphoto` (
`providerphoto_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`providerphoto_file` varchar(256) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`providerphoto_name` varchar(256) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`providerphoto_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`providerphoto_order` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`providerphoto_id`),
KEY `user_id` (`user_id`),
KEY `providerphoto` (`user_id`,`providerphoto_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=487 ;
-- --------------------------------------------------------
--
-- Table structure for table `review`
--
CREATE TABLE IF NOT EXISTS `review` (
`review_id` int(11) NOT NULL AUTO_INCREMENT,
`review_title` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`user_id` int(11) NOT NULL,
`review_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`review_content` text COLLATE utf8_unicode_ci NOT NULL,
`review_user_id` int(11) NOT NULL,
`review_rate` int(10) NOT NULL DEFAULT '1',
`review_tip` int(11) NOT NULL DEFAULT '0',
`service_booked` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`review_id`),
KEY `user_id` (`user_id`),
KEY `review_date` (`review_date`),
KEY `review_user_id` (`review_user_id`),
KEY `review_rate` (`review_rate`),
KEY `review_tip` (`review_tip`),
KEY `service_booked` (`service_booked`),
KEY `review` (`user_id`,`review_rate`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=97 ;
-- --------------------------------------------------------
--
-- Table structure for table `service`
--
CREATE TABLE IF NOT EXISTS `service` (
`service_id` int(11) NOT NULL AUTO_INCREMENT,
`service_name` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`service_created_by` int(11) NOT NULL DEFAULT '0',
`service_ICOC` varchar(256) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`service_price` int(11) NOT NULL,
`service_date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`service_date_expire` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`service_time` varchar(256) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
`service_rate` varchar(256) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
`service_type` int(10) NOT NULL DEFAULT '1' COMMENT '1-in call, 2-out call, 3-in&out call',
`service_recomanded` int(2) NOT NULL DEFAULT '0',
`service_genre` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`service_message` text COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`service_id`),
KEY `user_id` (`user_id`),
KEY `service_ICOC` (`service_ICOC`(255)),
KEY `service` (`user_id`,`service_price`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=854 ;
-- --------------------------------------------------------
--
-- Table structure for table `user`
--
CREATE TABLE IF NOT EXISTS `user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(256) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_password` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`user_email` varchar(256) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_phone` varchar(256) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_address1` text COLLATE utf8_unicode_ci,
`user_address2` text COLLATE utf8_unicode_ci NOT NULL,
`user_city` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`user_state` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`user_country` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`user_company` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`user_birthday` date DEFAULT NULL,
`user_register_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`user_type` enum('provider','client') COLLATE utf8_unicode_ci DEFAULT NULL,
`user_description` text COLLATE utf8_unicode_ci NOT NULL,
`user_available` int(10) NOT NULL DEFAULT '1',
`verified_email` tinyint(1) NOT NULL DEFAULT '0',
`verified_facebook` tinyint(1) NOT NULL DEFAULT '0',
`verified_phone` tinyint(1) NOT NULL DEFAULT '0',
`verified_twitter` tinyint(1) NOT NULL DEFAULT '0',
`user_facebook_friends` int(11) NOT NULL DEFAULT '0',
`user_twitter_friends` int(11) NOT NULL DEFAULT '0',
`user_longitude` decimal(10,5) NOT NULL DEFAULT '0.00000',
`user_latitude` decimal(10,5) NOT NULL DEFAULT '0.00000',
`user_deleted` tinyint(4) NOT NULL DEFAULT '0',
`user_gender` int(11) NOT NULL DEFAULT '0',
`user_facebook_token` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`user_active` tinyint(4) NOT NULL DEFAULT '1',
`user_location` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`user_push_notification_token` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`user_timezone_diff` int(11) NOT NULL DEFAULT '0',
`balanced_uri` text COLLATE utf8_unicode_ci NOT NULL,
`user_reset_passwd_token` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`is_test_user` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`user_id`),
KEY `deleted_idx` (`user_deleted`),
KEY `email_idx` (`user_email`(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=426 ;
You can probably just add indexes to speed up the query. Try adding the following indexes:
service(user_id, service_price)
providerphoto(user_id, providerphoto_order)
review(user_id, review_rate)
Something like this. This will return all the values for the users as I did not use the ORDER and LIMIT in the query. This is just for the approach.
SELECT
a.user_id as id,
user_name as name,
user_phone as phone,
user_email as email,
user_address1 as address1,
user_address2 as address2,
user_city as city,
user_state as state,
user_country as country,
user_available as available,
user_location as location,
user_latitude as latitude,
user_longitude as longitude,
user_description as description,
user_company as company,
user_gender as gender,
MIN(s.service_price) as price,
s.service_recomanded as recomandad,
verified_email,
verified_facebook,
verified_phone,
verified_twitter,
pp.providerphoto_name as photo,
ROUND( AVG(r.review_rate),2) as rate,
s.service_ICOC as type
FROM
user a LEFT JOIN service s on s.user_id = a.user_id LEFT JOIN providerphoto pp on pp.user_id = a.user_id LEFT JOIN review r on r.user_id = a.user_id
WHERE a.user_type = 'provider'
AND a.user_active=1
AND a.user_deleted=0;
First thing I would do is index user.user_type, user.user_deleted and user.user_active to let the optimizer quickly start with a smaller set of user records to have to deal with.
Do you mean to allow service.user_id to be NULL? That seems like a mistake.
Also you may want to throw indexes on any columns where you're doing an ORDER BY.
The best way to find out what is slow in this query is to do a EXPLAIN QUERY and analyze what it tells you. We can help you with that as well.