MySQL inner join and order by bad performance - performance

I have two tables. products and product_images.
Products: ~ 6 276 445 rows.
ProductImages: ~ 22 888 685 rows.
Table definitions:
products
CREATE TABLE `products` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`unique_id` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
`feed_id` bigint(20) unsigned NOT NULL,
`feed_item_id` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`item_group_id` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`name` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL,
`slug` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL,
`description` text COLLATE utf8mb4_unicode_ci NOT NULL,
`categorytext` text COLLATE utf8mb4_unicode_ci NOT NULL,
`categorytext_hash` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
`manufacturer` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`product_url` text COLLATE utf8mb4_unicode_ci NOT NULL,
`price_vat` float(12,2) DEFAULT NULL,
`price_vat_old` float(12,2) DEFAULT NULL,
`vat` tinyint(4) DEFAULT NULL,
`discount_percentage` tinyint(4) NOT NULL DEFAULT 0,
`image_source_url` text COLLATE utf8mb4_unicode_ci NOT NULL,
`image_filename` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL,
`gtin` varchar(14) COLLATE utf8mb4_unicode_ci NOT NULL,
`ean` varchar(13) COLLATE utf8mb4_unicode_ci NOT NULL,
`isbn` varchar(13) COLLATE utf8mb4_unicode_ci NOT NULL,
`upc` varchar(12) COLLATE utf8mb4_unicode_ci NOT NULL,
`mpn` varchar(70) COLLATE utf8mb4_unicode_ci NOT NULL,
`missing_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`deleted_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`project_1` tinyint(4) NOT NULL DEFAULT 0,
`project_2` tinyint(4) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_id_UNIQUE` (`unique_id`),
KEY `feed_id_updated_at` (`feed_id`,`updated_at`),
KEY `feed_id` (`feed_id`),
KEY `feed_id_missing_at` (`feed_id`,`missing_at`),
KEY `feed_id_categorytext_hash_id` (`feed_id`,`categorytext_hash`,`id`),
KEY `categorytext_hash` (`categorytext_hash`),
KEY `missing_at_deleted_at_categorytext_hash_feed_id_id` (`missing_at`,`deleted_at`,`categorytext_hash`,`feed_id`,`id`),
KEY `project_1_id` (`project_1`,`id`),
KEY `project_2_id` (`project_2`,`id`),
KEY `project_1` (`project_1`),
KEY `project_2` (`project_2`),
CONSTRAINT `fk_products_feeds` FOREIGN KEY (`feed_id`) REFERENCES `feeds` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=122268834 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
product_images
CREATE TABLE `product_images` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`product_unique_id` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
`unique_id` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
`feed_id` bigint(20) unsigned NOT NULL,
`image_source_url` text COLLATE utf8mb4_unicode_ci NOT NULL,
`image_filename` varchar(200) COLLATE utf8mb4_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',
`deleted_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `unique_id_UNIQUE` (`unique_id`),
KEY `feed_id_updated_at` (`feed_id`,`updated_at`),
KEY `product_unique_id` (`product_unique_id`),
KEY `product_unique_id_id` (`product_unique_id`,`id`),
CONSTRAINT `fk_product_images_products1` FOREIGN KEY (`product_unique_id`) REFERENCES `products` (`unique_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=333584333 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
My Query without ORDER BY
SELECT IMG.id
FROM products PR
INNER JOIN product_images IMG on IMG.product_unique_id = PR.unique_id
WHERE
PR.project_1 = 1
-- ORDER BY IMG.id
LIMIT 1000 OFFSET 0
Duration: 0.016 sec
EXPLAIN:
id|select_type|table|type|possible_keys |key |key_len|ref |rows |Extra |
--+-----------+-----+----+---------------------------------------+-----------------+-------+-----------------------------+------+-----------+
1|SIMPLE |PR |ref |unique_id_UNIQUE,project_1_id,project_1|project_1 |1 |const |286960| |
1|SIMPLE |IMG |ref |product_unique_id,product_unique_id_id |product_unique_id|130 |products-storage.PR.unique_id|2 |Using index|
My Query with ORDER BY
SELECT IMG.id
FROM products PR
INNER JOIN product_images IMG on IMG.product_unique_id = PR.unique_id
WHERE
PR.project_1 = 1
ORDER BY IMG.id
LIMIT 1000 OFFSET 0
Duration 17.922 sec
EXPLAIN:
id|select_type|table|type|possible_keys |key |key_len|ref |rows |Extra |
--+-----------+-----+----+---------------------------------------+-----------------+-------+-----------------------------+------+-------------------------------+
1|SIMPLE |PR |ref |unique_id_UNIQUE,project_1_id,project_1|project_1 |1 |const |286960|Using temporary; Using filesort|
1|SIMPLE |IMG |ref |product_unique_id,product_unique_id_id |product_unique_id|130 |products-storage.PR.unique_id|2 |Using index |
Problem
Query with order by is too slow. Is there a way to speed up query with some index or server settings? I need order by for pagination.
The goal is to select all images for project and send them through the api to another server.
I'm using mariadb 10.8.2
Server is on SSD with 8GB RAM

Related

How can we replace value in unix using sed after a certain pattern

I Have a file with below data, in which I want to replace everything in line after a Default nextval with AUTO_INCREMENT
I tries sed command but it didn't help
Data:
`id` bigint(20) NOT NULL DEFAULT nextval(`xchange_finance`.`seq_cancelled_contract_zendesk_publishing_failure`),
`id` bigint(20) NOT NULL DEFAULT nextval(`xchange_finance`.`seq_invoice`),
`id` bigint(20) NOT NULL DEFAULT nextval(`xchange_finance`.`seq_invoice_attachment`),
`id` bigint(20) NOT NULL DEFAULT nextval(`xchange_finance`.`seq_invoice_line_item`),
expected:
`id` bigint(20) NOT NULL AUTO_INCREMENT ,
`id` bigint(20) NOT NULL AUTO_INCREMENT ,
`id` bigint(20) NOT NULL AUTO_INCREMENT ,
`id` bigint(20) NOT NULL AUTO_INCREMENT
tried something like below but it didn't work
sed -e 's/^DEFAULT nextval.*$/AUTO_INCREMENT ,/g' test

C# Error ORA 00907: Missing Right Parenthesis

My error:
ORA 00907: Missing Right Parenthesis
My code:
CREATE TABLE TBL_TD_USER
(
USER_ID INTEGER(10) NOT NULL,
USER_NAME VARCHAR2(20) NOT NULL,
PASSWORD VARCHAR2(20) NOT NULL,
CREATED_BY VARCHAR2(20) NOT NULL,
CREATED_DATE DATE NOT NULL,
MODIFIED_BY VARCHAR2(20) NOT NULL,
MODIFIED_DATE DATE NOT NULL,
IS_ACTIVE CHARACTER(1) NOT NULL,
DESCRIPTION VARCHAR2(200) NOT NULL,
CONSTRAINT TBL_TD_USER PRIMARY KEY (USER_ID)
);
This works:
CREATE TABLE TBL_TD_USER
(
USER_ID INTEGER NOT NULL ,
USER_NAME VARCHAR2(20) NOT NULL,
PASSWORD VARCHAR2(20) NOT NULL,
CREATED_BY VARCHAR2(20) NOT NULL,
CREATED_DATE DATE NOT NULL,
MODIFIED_BY VARCHAR2(20) NOT NULL,
MODIFIED_DATE DATE NOT NULL,
IS_ACTIVE VARCHAR2(1) NOT NULL,
DESCRIPTION VARCHAR2(200) NOT NULL,
CONSTRAINT TBL_TD_USER PRIMARY KEY (USER_ID)
);
There's no variable type named CHARACTER in oracle, as an alternative, you to use VARCHAR2 with length value in parenthesis.
You can use INTEGER type without length.
demo

how to insert using timestamp oracle

I made a 'Kereta' table:
CREATE TABLE Kereta (
ID_Kereta NUMBER(3) CONSTRAINT kr_id_kr_pk PRIMARY KEY,
Nama_Kereta VARCHAR2(30) CONSTRAINT kr_nama_kr_nn NOT NULL,
Jam_Keberangkatan TIMESTAMP CONSTRAINT kr_jk_nn NOT NULL,
Jam_Tiba TIMESTAMP CONSTRAINT kr_jt_nn NOT NULL,
Stasiun_Asal VARCHAR2(20) CONSTRAINT kr_sa_nn NOT NULL,
Stasiun_Tujuan VARCHAR2(20) CONSTRAINT kr_st_nn NOT NULL
)
Then i tried to insert 13.00 to Jam_Keberangkatan field. I can't insert one by one because of NOT NULL constraint, what should i do?

Spring Data Jpa JPQL using inner join to select minimum priced holiday

I am trying to find the minimum priced holiday for each month from each departure airport to a target destination airport from a table with over 1 million records. I have successfully created the required MySQL query :-
SELECT * FROM results r INNER JOIN (SELECT id,MIN(price) FROM results GROUP BY BulkLoaderRef) r2 ON r.id=r2.id ORDER BY r.departureDate, r.depAirportCode, r.resortID LIMIT 10
But I cannot find a valid JPQL syntax for this query?
Help please?
My table format is as follows :-
CREATE TABLE `results` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`HotelReference` varchar(128) NOT NULL DEFAULT '',
`HotelName` varchar(64) NOT NULL DEFAULT '',
`Resort` varchar(128) NOT NULL DEFAULT '',
`Description` longtext NOT NULL,
`Photo` varchar(255) NOT NULL DEFAULT '',
`OfferProcessType` varchar(1) NOT NULL DEFAULT 'A',
`TradingName` varchar(32) NOT NULL DEFAULT 'Heffernans Travel',
`OfferLabelDesc` varchar(16) NOT NULL DEFAULT 'EasyHolidays',
`DestinationName` varchar(128) NOT NULL DEFAULT '',
`DepAirportCode` varchar(3) NOT NULL DEFAULT '',
`TelephoneNumber` varchar(16) NOT NULL DEFAULT '021 230 0700',
`ReturnAirportCode` varchar(3) NOT NULL DEFAULT '',
`ArrivalAirportCode` varchar(3) NOT NULL DEFAULT '',
`ResortID` int(11) NOT NULL,
`ABTA` varchar(16) NOT NULL DEFAULT 'ABTA',
`ATOL` varchar(16) NOT NULL DEFAULT 'ATOL',
`OtherBonding` varchar(64) NOT NULL DEFAULT 'Agent for ATOL protected operator',
`BoardTypesShort` varchar(2) NOT NULL DEFAULT '',
`AccomodationShort` varchar(3) NOT NULL DEFAULT 'Htl',
`AllocationShort` varchar(3) DEFAULT '""',
`OperatorShort` varchar(16) NOT NULL DEFAULT 'EasyHolidays',
`DepartureDate` date NOT NULL,
`DepartureMonth` varchar(2) DEFAULT NULL,
`DepartureYear` varchar(4) DEFAULT NULL,
`Price` int(11) NOT NULL,
`ShareBasisAdult` int(11) DEFAULT '2',
`BulkLoaderRef` varchar(64) DEFAULT NULL,
`Email` varchar(64) DEFAULT '',
`PublicNotes` varchar(64) DEFAULT 'EasyHolidays',
`CCC` varchar(32) DEFAULT ' Credit Card Charges',
`TOD` varchar(16) DEFAULT '',
`DCC` varchar(16) DEFAULT '',
`AMEX` varchar(16) DEFAULT '',
`CDW` varchar(16) DEFAULT '',
`BookingFee` double DEFAULT NULL,
`AccomodationName` varchar(64) NOT NULL DEFAULT '',
`duration` int(11) NOT NULL DEFAULT '0',
`ShareBasisChild` int(11) DEFAULT '2',
`Rating` varchar(16) NOT NULL DEFAULT '',
`OfferLink` varchar(16) NOT NULL DEFAULT '',
`AccomodationRef` varchar(128) NOT NULL DEFAULT '',
`Transfers` varchar(32) NOT NULL DEFAULT '',
`OperatorsRating` varchar(32) NOT NULL DEFAULT '',
`QuoteRef` varchar(64) NOT NULL DEFAULT '',
`Url` varchar(128) NOT NULL DEFAULT '',
`OutDepTime` varchar(16) NOT NULL DEFAULT '',
`OutArrTime` varchar(16) NOT NULL DEFAULT '',
`InDepTime` varchar(16) NOT NULL DEFAULT '',
`InArrTime` varchar(16) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `HotelReference` (`HotelReference`),
KEY `BulkLoaderRef` (`BulkLoaderRef`),
KEY `Price` (`Price`)
) ENGINE=InnoDB AUTO_INCREMENT=1091073 DEFAULT CHARSET=utf8;
JPQL doesn't support sub-queries for JOIN statements. With that said, I would suggest that you move the subquery of your JOIN into a view. Then it becomes a simple JPQL statement.
Move subquery into a view:
CREATE VIEW min_price_view AS
SELECT id,MIN(price) FROM results GROUP BY BulkLoaderRef;
Then replace in query:
SELECT
*
FROM
results r
INNER JOIN
min_price_view r2 ON r.id=r2.id
ORDER BY
r.departureDate, r.depAirportCode, r.resortID
LIMIT 10
At this point, it becomes very simple to convert it to JPQL.

Error MySQL #1064 at 1 Line

I need to create table for my sql database and i have a 1 error sucking my work, anyone can help me for i fix that error:
[sql]CREATE TABLE `z_ots_comunication` (
`id` int(0) NOT NULL DEFAULT 0
`name` varchar(255) NOT NULL,
`type` varchar(255) NOT NULL,
`action` varchar(255) NOT NULL,
`param1` varchar(255) NOT NULL,
`param2` varchar(255) NOT NULL,
`param3` varchar(255) NOT NULL,
`param4` varchar(255) NOT NULL,
`param5` varchar(255) NOT NULL,
`param6` varchar(255) NOT NULL,
`param7` varchar(255) NOT NULL,
`delete_it` int(2) NOT NULL default '1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Msg of MySQL:
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 '[sql]CREATE TABLE z_ots_comunication (
id int(0) NOT NULL DEFAULT 0
name' at line 1
You forgot to put a comma after the ID column:
CREATE TABLE `z_ots_comunication` (
`id` int(0) NOT NULL DEFAULT 0,
`name` varchar(255) NOT NULL,
`type` varchar(255) NOT NULL,
...
Try This code u have missed a semi colon before name filed
CREATE TABLE `z_ots_comunication` (
`id` int(0) NOT NULL DEFAULT 0,
`name` varchar(255) NOT NULL,
`type` varchar(255) NOT NULL,
`action` varchar(255) NOT NULL,
`param1` varchar(255) NOT NULL,
`param2` varchar(255) NOT NULL,
`param3` varchar(255) NOT NULL,
`param4` varchar(255) NOT NULL,
`param5` varchar(255) NOT NULL,
`param6` varchar(255) NOT NULL,
`param7` varchar(255) NOT NULL,
`delete_it` int(2) NOT NULL default '1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

Resources