Optimizing MySQL queries / database structure - sql

I have in my MySQL database these two tables:
CREATE TABLE IF NOT EXISTS `articles` (
`id` bigint(20) NOT NULL,
`url` varchar(255) collate utf8_bin NOT NULL,
`img` varchar(255) collate utf8_bin NOT NULL,
`name` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
`url_key` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
`type_code` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
`likes_count` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `type` (`type`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE IF NOT EXISTS `articles_types` (
`id` int(11) NOT NULL auto_increment,
`code` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
`url_key` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
`name` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`code`),
KEY `type` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;
And I'm trying to run this query:
SELECT a.* FROM `articles` a INNER JOIN articles_types at ON at.`code`=a.type ORDER BY p.likes_count DESC LIMIT 1
Which returns 1 row, in 1sec.
The table articles consists of aproximately over 70k rows, articles_types only about 70 rows.
Is there any chance to optimize the structure (maybe engine?) of that tables, or optimize the query itself to make it faster?

You should examine the output of EXPLAIN on your query
It's likely the order by that gets you, so create an index on articles.likes_count

Related

Having trouble adding a foreign key in HeidiSQL (Error 1215)

I've tried just about everything but I'm getting error 1215 when trying to create a foreign key in a child table I have. Here are my tables:
CREATE TABLE `Con` (
`ConID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(250) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`Website` varchar(500) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`FirstYear` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`ConID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `ConEvent` (
`EventID` int(11) NOT NULL AUTO_INCREMENT,
`ConID` int(11) NOT NULL,
`DateStart` date DEFAULT NULL,
`DateEnd` date DEFAULT NULL,
`Year` tinyint(4) DEFAULT NULL,
`Venue` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
`Address` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
`City` tinytext COLLATE utf8_unicode_ci,
`StateProvince` tinytext COLLATE utf8_unicode_ci,
`Country` tinytext COLLATE utf8_unicode_ci,
PRIMARY KEY (`EventID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Here is my syntax:
ALTER TABLE ConEvent
ADD FOREIGN KEY (ConID) REFERENCES Con(ConID);
I can't SHOW ENGINE INNODB STATUS; because I'm not a super user (error 1227). I tried to make myself one but was unable to.

How to resolve 'Foreign key constraint is incorrectly formed' issue

I am trying to create a database table called wp_tokens with a foreign key relationship to another table called wp_users. However, every time I attempt to run the create table SQL, I get the error "Foreign key constraint is incorrectly formed". I have tried multiple re-edits of the same code, but I just cannot figure out what is going on.
This is the code for wp_users
CREATE TABLE `wp_users` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_login` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_pass` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_nicename` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_url` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_activation_key` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_status` int(11) NOT NULL DEFAULT '0',
`display_name` varchar(250) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`ID`),
KEY `user_login_key` (`user_login`),
KEY `user_nicename` (`user_nicename`),
KEY `user_email` (`user_email`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci
This is the SQL for wp_tokens
CREATE TABLE `wp_tokens` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NOT NULL,
`token` varchar(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES wp_users (`ID`)
)
Any help would be greatly appreciated!
The unsigned is important -- the types need to be identical. Try this:
CREATE TABLE `wp_tokens` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) unsigned NOT NULL,
`token` varchar(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES wp_users (`ID`)
)
Here is a rextester.

Mapping joined query result into Eloquent model

I have following database schema:
CREATE TABLE `languages` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`code` char(2) COLLATE utf8_unicode_ci NOT NULL,
`latin_name` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`local_name` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `languages_code_unique` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `films` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `films_downloads` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`film_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `films_downloads_film_id_foreign` (`film_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `films_downloads_langs` (
`download_id` int(10) unsigned NOT NULL,
`language_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`download_id`,`language_id`),
KEY `films_downloads_langs_language_id_foreign` (`language_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I would get all Languages by film for films list and use it next like
foreach ($films as $film) $film->languages;
SQL query and following code in Laravel allow me get what I want, but
in flat array.
$q = DB::table('films)
->select('languages.latin_name', 'films.id as film_id')
->join('films_downloads', 'films.id', '=', 'films_downloads.app_id')
->join('films_downloads_langs', 'films_downloads.id', '=', 'films_downloads_langs.download_id')
->join('languages', 'languages.id', '=', 'films_downloads_langs.language_id')
->whereIn('films_downloads.film_id', $films_ids)
->groupBy('languages.id' ,'films_downloads.app_id')
->get();
I would nested arrays / collections / models (prefer), with grouping repeating languages in mysql for multiple films at one query. How I can fill Film models collection by languages collections?

Why is the comma messing up the insert

here is my query
insert into invoices
set Invoice_number = '823N9823',
price = '11,768.00',
code = 'ret_4_business',
created_at = '2010-09-27';
but my price in my db is 11, not 11768
how do i handle money in mysql? the field is a decimal
CREATE TABLE `invoices` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Invoice_number` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`price` decimal(10,0) DEFAULT NULL,
`code` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Any non-number character will terminate the parsing and save only part of the number. Remove the comma characters from the query.

mass replace of values in more tables by a highest value

I have a table with OWN_ID and OWN_Email -
own_id | own_email
-----------------------------------------------
3ace7cf80edd | email#example.com
3acf6af33ff7 | email#example.com
3acda2524e00 | email#example.com
3ad75583c9a7 | spam#example.com
3ad74b018999 | spam#example.com
etc.
the problem is that it should contain only a single ID per Email, also I need to replace all OWN_ID values in another table by highest OWN_ID value of the OWN_Email
sql create:
CREATE TABLE `blahblah.`eventowner` (
`OWN_ID` varchar(12) COLLATE utf8_czech_ci NOT NULL DEFAULT '',
`OWN_Email` varchar(120) COLLATE utf8_czech_ci DEFAULT NULL,
`OwnDateFormat` varchar(16) COLLATE utf8_czech_ci DEFAULT NULL,
`OwnWeekStart` int(11) DEFAULT NULL,
`OwnDayStart` int(11) DEFAULT NULL,
`OwnDayEnd` int(11) DEFAULT NULL,
PRIMARY KEY (`OWN_ID`),
KEY `OwnerEmailIndex` (`OWN_Email`),
KEY `OwnerIndex` (`OWN_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci
CREATE TABLE `blahblah`.`event` (
`EVN_ID` varchar(128) COLLATE utf8_czech_ci DEFAULT NULL,
`EVNGRP_ID` varchar(12) COLLATE utf8_czech_ci DEFAULT NULL,
`EVNOWN_ID` varchar(12) COLLATE utf8_czech_ci DEFAULT NULL,
`EVNRCR_ID` varchar(12) COLLATE utf8_czech_ci DEFAULT NULL,
`Evn_EditCounter` int(11) DEFAULT NULL,
`Evn_Created` int(11) DEFAULT NULL,
`Evn_Modified` int(11) DEFAULT NULL,
`EvnFolder` varchar(128) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnTitle` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnNote` text COLLATE utf8_czech_ci,
`EvnLocation` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnPriority` int(11) DEFAULT NULL,
`EvnComplete` int(11) DEFAULT NULL,
`EvnColor` varchar(2) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnClass` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnShareType` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnTimeFormat` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnType` varchar(127) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnStatus` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnOrganizer` varchar(80) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnContact` varchar(80) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnURL` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnStartDate` int(11) DEFAULT NULL,
`EvnStartTime` int(11) DEFAULT NULL,
`EvnEndDate` int(11) DEFAULT NULL,
`EvnEndTime` int(11) DEFAULT NULL,
`EvnRID` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnUID` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
`EvnExpire` int(11) DEFAULT NULL,
`EvnSequence` int(11) DEFAULT NULL,
`EvnFlags` int(11) DEFAULT NULL,
KEY `EventGroupClassIndex` (`EVNGRP_ID`,`EvnClass`),
KEY `EventGroupFolderIndex` (`EVN_ID`,`EVNGRP_ID`,`EvnFolder`),
KEY `EventRIDIndex` (`EvnRID`),
KEY `EventUIDIndex` (`EvnUID`),
KEY `EventGroupRCRIndex` (`EVN_ID`,`EVNGRP_ID`,`EVNRCR_ID`),
KEY `EventExpireIndex` (`EvnExpire`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci
delete t1 from table t1, table t2
where t1.own_email= t2.own_email
and t1.own_id < t2.own_id
update othertable, table
set othertable.own_id= table.own_id
where othertable.own_email= table.own_email;
Could accomplish this fairly simple using a transaction that nukes the whole table.
BEGIN;
SELECT own_email, max(own_Id) INTO temptable FROM table GROUP BY own_email
TRUNCATE table;
INSERT INTO table SELECT * FROM temptable;
DELETE temptable;
COMMIT;
But, that Isn't the best way -- you can also do it by deleting just the right rows.
DELETE FROM table AS d WHERE EXISTS (
SELECT own_id FROM table AS t
WHERE NOT EXISTS (
SELECT max(own_id) FROM table AS i
GROUP BY own_email
WHERE t.own_email = i.own_email
)
AND d.own_id = t.own_id
AND d.own_email = t.own_email
);
Here we delete all own_ids, that aren't the max(own_id) for an E-mail.
This can be accomplished with NOT IN (understanding the caveat on nulls), or an anti-join on the table being deleted from