mysql INSERT abysmal performance - mysql

I'm iterating over about 14,000 records which I get from csv file.
For each user I do this:
1) I create a user record in users table
2) I insert ~21 attributes for that user into users_attributes table
Initially each user takes ~2 seconds to be inserted, but after awhile insertion time grows to ~50 seconds... and so script takes forever....
Why is it so slow?
Example insert statement:
Executed once:
INSERT INTO users (uname, email, pass, passreminder, activated, approved_date, approved_by, user_regdate, lastlogin, theme, ublockon, ublock, tz, locale) VALUES ('someid_example.com', 'someid#example.com', 'test', 'User-generated', -32768, '2014-09-03 14:41:50', 2, '2014-09-03 14:41:50', '1970-01-01 00:00:00', '', 0, '', '', '');
Repeated 21 times:
INSERT INTO users_attributes (name, value, user_id) VALUES ('user_information_address', 'some address', 9862);
Schema:
CREATE TABLE `users` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`uname` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`pass` varchar(138) COLLATE utf8_unicode_ci NOT NULL,
`passreminder` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`activated` smallint(6) NOT NULL,
`approved_date` datetime NOT NULL,
`approved_by` int(11) NOT NULL,
`user_regdate` datetime NOT NULL,
`lastlogin` datetime NOT NULL,
`theme` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`ublockon` smallint(6) NOT NULL,
`ublock` longtext COLLATE utf8_unicode_ci NOT NULL,
`tz` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`locale` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`uid`),user
KEY `uname` (`uname`),
KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `users_attributes` (
`user_id` INT(11) NOT NULL,
`name` VARCHAR(80) COLLATE utf8_unicode_ci NOT NULL,
`value` longtext COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`user_id`,`name`),
KEY `IDX_SOMENUM` (`user_id`),
CONSTRAINT `IDX_SOMENUM` FOREIGN KEY (`user_id`) REFERENCES `users` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Related

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: ERROR 1072 (42000) at line.. Key column UserID doesn't exist in the table

I am trying to make simple database with 2 tables, first for user information, and second for their uploads, because it's project for faculty, I have some assignments... And one is to use foreign key.
But I can't figure out how to do that, when I do it with phpMyAdmin everything is working just fine, but when I export it and put it on my server I have some errors; please help me.
CREATE TABLE IF NOT EXISTS `korisnici` (
`UserID` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(12) COLLATE utf8_bin NOT NULL,
`password` varchar(32) COLLATE utf8_bin NOT NULL,
`email` varchar(32) COLLATE utf8_bin NOT NULL,
`telefon` varchar(16) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`UserID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin ROW_FORMAT=COMPACT AUTO_INCREMENT=4;
CREATE TABLE IF NOT EXISTS `slike` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) COLLATE utf8_bin NOT NULL,
`size` int(11) NOT NULL,
`type` varchar(200) COLLATE utf8_bin NOT NULL,
`file_path` varchar(200) COLLATE utf8_bin NOT NULL,
`username` varchar(12) COLLATE utf8_bin NOT NULL,
`naslov` varchar(32) COLLATE utf8_bin NOT NULL,
`adresa` varchar(80) COLLATE utf8_bin NOT NULL,
`opis` varchar(1200) COLLATE utf8_bin NOT NULL,
`datum` date NOT NULL,
`UserID` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `UserID` (`UserID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=4 ;
ALTER TABLE `slike`
ADD CONSTRAINT `slike_ibfk_1` FOREIGN KEY (`UserID`) REFERENCES `korisnici` (`UserID`);
ERRORS:
ERROR 1072 (42000) at line 77: Key column 'UserID' doesn't exist in table
I would appreciate if someone could fix my code and explain.
EDIT new error:
ERROR 1054 (42S22) at line 42: Unknown column 'UserID' in 'field list'
Again when I insert something:
SET FOREIGN_KEY_CHECKS=0;
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE IF NOT EXISTS `korisnici` (
`username` varchar(12) COLLATE utf8_bin NOT NULL,
`password` varchar(32) COLLATE utf8_bin NOT NULL,
`email` varchar(32) COLLATE utf8_bin NOT NULL,
`telefon` varchar(16) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
INSERT INTO `korisnici` (`username`, `password`, `email`, `telefon`) VALUES('Aleksa2', 'c4b4d0dea25b6f2f38fef63330ea15c8', 'sss#gmail.com', '0649999999');
INSERT INTO `korisnici` (`username`, `password`, `email`, `telefon`) VALUES('Aleksa123', 'a4b4d0dea25b6f2f38fef63330ea15c8', 'aaaa#gmail.com', '0649999999');
CREATE TABLE IF NOT EXISTS `slike` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) COLLATE utf8_bin NOT NULL,
`size` int(11) NOT NULL,
`type` varchar(200) COLLATE utf8_bin NOT NULL,
`file_path` varchar(200) COLLATE utf8_bin NOT NULL,
`username` varchar(12) COLLATE utf8_bin NOT NULL,
`naslov` varchar(32) COLLATE utf8_bin NOT NULL,
`adresa` varchar(80) COLLATE utf8_bin NOT NULL,
`opis` varchar(1200) COLLATE utf8_bin NOT NULL,
`datum` date NOT NULL,
PRIMARY KEY (`id`),
KEY `USERNAME_ID` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=3 ;
INSERT INTO `slike` (`id`, `name`, `size`, `type`, `file_path`, `username`, `naslov`, `adresa`, `opis`, `datum`) VALUES(15, '4eb40ffebd3fbec4b1acf34a4fe8cb2d.png', 43395, 'image/png', 'C:/Program Files (x86)/EasyPHP-DevServer-14.1VC11/data/localweb/images/4eb40ffebd3fbec4b1acf34a4fe8cb2d.png', 'Aleksa123', 'asdasdasd', 'dadasdas', 'Opisite problem u najvise 1200 karaktera.', '2014-09-05');
INSERT INTO `slike` (`id`, `name`, `size`, `type`, `file_path`, `username`, `naslov`, `adresa`, `opis`, `datum`) VALUES(16, '92bc0821619c053c503694666f2717ee.png', 32461, 'image/png', 'C:/Program Files (x86)/EasyPHP-DevServer-14.1VC11/data/localweb/images/92bc0821619c053c503694666f2717ee.png', 'Aleksa123', 'dadasdasdas', 'dadasdasdasdasdas', 'Opisite problem u najvise 12dasdasdasdas00 karaktera.', '2014-09-05');
INSERT INTO `slike` (`id`, `name`, `size`, `type`, `file_path`, `username`, `naslov`, `adresa`, `opis`, `datum`) VALUES(17, '89d0507bf3ed3f492647b7fd2f39047a.png', 162203, 'image/png', 'C:/Program Files (x86)/EasyPHP-DevServer-14.1VC11/data/localweb/images/89d0507bf3ed3f492647b7fd2f39047a.png', 'Aleksa123', '1111111111111a', 'dasdasdasda', 'Opisite problsdasdasdasdasaem u najvise 1200 karaktera.', '2014-09-05');
ALTER TABLE `slike`
ADD CONSTRAINT `slike_ibfk_1` FOREIGN KEY (`username`) REFERENCES `korisnici` (`username`);
SET FOREIGN_KEY_CHECKS=1;

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.

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'xxx' for key 'group_key'

On my web page there is no insert or update but already to this I'm getting this error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0-Bekoo-2449.10' for key 'group_key'
This is surprising because the web page does not modify any rows, and the Product table does not have a group_key column or key.
My query :
SELECT Product._like,
comment_count,
title,
price_lower,
price,image,
AffiliateOffers.name,
p‌​‌​ayout_yuzde,
payout_nakit,
payout_type,
xml_id,
brand,model,
currency,url,
Product.af‌​f_‌​id,
Product.offer_id,
pb_share_1,
pb_share_2,
pb_share_1_payda,
pb_share_2_payda,
A‌​ffil‌​iateOffers.seo_title,
afo_offer_id,
r_category,
seo_description
FROM Product
inner Join AffiliateOffers on Product.aff_id = AffiliateOffers.af_id
AND Product.offer_id = AffiliateOffers.af_offer_id
where Product.status = 1
and Product.aff_id = 1
and Product.offer_id = 1290
and my table structure :
` CREATE TABLE `_product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`xml_id` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`aff_id` int(11) NOT NULL,
`offer_id` int(11) NOT NULL,
`r_category` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`category_id1` int(11) NOT NULL,
`category_id2` int(11) NOT NULL,
`category_id3` int(11) NOT NULL,
`brand` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`model` varchar(75) COLLATE utf8_unicode_ci DEFAULT NULL,
`title` mediumtext COLLATE utf8_unicode_ci NOT NULL,
`description_1` mediumtext COLLATE utf8_unicode_ci,
`price` decimal(18,2) NOT NULL,
`price_lower` decimal(18,2) NOT NULL,
`percentage_lower` int(11) NOT NULL,
`image` mediumtext COLLATE utf8_unicode_ci NOT NULL,
`url` text COLLATE utf8_unicode_ci,
`feature` tinyint(4) NOT NULL,
`city` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
`sex` tinyint(4) NOT NULL,
`stock` tinyint(4) NOT NULL,
`start_date` int(25) NOT NULL,
`finish_date` int(25) NOT NULL,
`update_date` int(25) NOT NULL,
`status` tinyint(4) NOT NULL DEFAULT '1',
`hit` int(11) NOT NULL DEFAULT '1',
`_like` int(11) NOT NULL DEFAULT '0',
`comment_count` int(11) DEFAULT NULL,
`like_count` int(11) DEFAULT NULL,
`lastlike_time` int(25) DEFAULT NULL,
`visit_count` int(11) DEFAULT NULL,
`lastvisit_count` int(25) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `xml_id` (`xml_id`),
KEY `lastlike_time` (`lastlike_time`),
KEY `offer_id` (`offer_id`),
KEY `r_category` (`r_category`),
KEY `status` (`status`)
) ENGINE=MyISAM AUTO_INCREMENT=3816867 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`
Why am I getting this error?
Unless you say otherwise, group_key is an internal field used by MySQL when aggregating things so you won't find it in your own tables or view. A duplicate entry error in there means you've got some sort of problem with your aggregation.
Have a look at any GROUP BY statements you're using, and ensure you've got their syntax correct, particularly in any aggregate functions and group expressions.
In Phpmyadmin you have to truncate all the logtables.
That should fix it.
I ran into the same problem, i was forgotten to clear the visitor and online log.
Both of the tables are generate a log/id for the user ( thats where the XXX for stands )
If you truncate both of the tables it will fix this problem.

Synchronize Table data with a whole different structure

The goal is to synchronize the customers data and his table structure to our table structure.
The table structure is the following:
CREATE TABLE IF NOT EXISTS `BildSerie_W2L` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`BildserieID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Prislista` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`BildKod` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`AvdKlassID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Status` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=24 ;
CREATE TABLE IF NOT EXISTS `classes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`client_id` int(10) unsigned NOT NULL,
`class` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`year` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `classes_client_id_foreign` (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=9 ;
CREATE TABLE IF NOT EXISTS `Elever_W2L` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`X_ElevID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Efternamn` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Förnamn` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`GataMedNr` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`PostNr` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Postort` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`TelefonMobil` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Epost1` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Epost2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`X_AvdKlassId` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Status` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=18 ;
CREATE TABLE IF NOT EXISTS `Elev_Bild_BildSerie_W2L` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Elev_Bild_Bildserie_ID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`BildSerieID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`ElevID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Status` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=26 ;
CREATE TABLE IF NOT EXISTS `image_codes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`client_id` int(10) unsigned DEFAULT NULL,
`code` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `image_codes_code_unique` (`code`),
KEY `image_codes_client_id_foreign` (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=20 ;
CREATE TABLE IF NOT EXISTS `image_series` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`image_code_id` int(10) unsigned NOT NULL,
`pricelist_id` int(10) unsigned NOT NULL DEFAULT '1',
`class_id` int(10) unsigned DEFAULT NULL,
`image_serie` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`year` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `image_series_image_code_id_foreign` (`image_code_id`),
KEY `image_series_pricelist_id_foreign` (`pricelist_id`),
KEY `image_series_class_id_foreign` (`class_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
The goal is to take the data in "BildSerie_W2L" and populate it in to image_series.
My query looks like this:
SELECT `BildSerie_W2L`.*, `classes`.id, `image_codes`.id as ImageID, `Elever_W2L`.Förnamn, `Elever_W2L`.Efternamn
FROM `BildSerie_W2L`
INNER JOIN `image_codes` ON `BildSerie_W2L`.BildKod = `image_codes`.code
INNER JOIN `classes` ON `BildSerie_W2L`.AvdKlassID = `classes`.class
INNER JOIN `Elev_Bild_BildSerie_W2L` ON `Elev_Bild_BildSerie_W2L`.BildSerieID = `BildSerie_W2L`.BildserieID
INNER JOIN `Elever_W2L` ON `Elever_W2L`.X_ElevID = `Elev_Bild_BildSerie_W2L`.ElevID
WHERE `BildSerie_W2L`.Status = 0
However it gives back duplicated results and not all the data in BildSerie_W2L...