mysql query tree struct with mptt slower than 2D relational table - mysql

My business scenario is shown in the figure above. A user can create multiple products, a product can have multiple modules, and a module can have multiple parameters. The parameters include variable types, variable names, and variable values.
Before starting I thought the query speed of mptt was better than 2D relational table, but the result is completely opposite.
I now have two data table designs.
Option One:
CREATE TABLE `products` (
`product_id` bigint(20) NOT NULL AUTO_INCREMENT,
`product_name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`cfdversion` bigint(20) NULL DEFAULT NULL,
`product_info` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`is_activated` tinyint(1) NULL DEFAULT NULL,
PRIMARY KEY (`product_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 226 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
CREATE TABLE `person_param` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` bigint(20) NULL DEFAULT NULL,
`param_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`var_type` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`var_value` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`var_name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`is_activated` tinyint(1) NULL DEFAULT 1,
`compute_value` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`module_name` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `product_id`(`product_id`) USING BTREE,
CONSTRAINT `person_param_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`product_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 19 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
I connect the id of the product information with the parameter table.
Option two:
products table is same just table name is different.
person_paramlike this:
CREATE TABLE `mptt_param` (
`node_id` bigint(20) NOT NULL AUTO_INCREMENT,
`node_name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`lft` bigint(20) NOT NULL,
`rgt` bigint(20) NOT NULL,
`node_level` bigint(20) NOT NULL,
PRIMARY KEY (`node_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
I added 200 products with 3 modules per product and 10 parameters per module.
option 1's sql statement:SELECT * from `products` a RIGHT JOIN `person_param` b ON a.product_id=b.product_id WHERE a.product_id=246;
option 2's sql statement:SELECT * FROM mptt_param WHERE lft>=(SELECT lft FROM mptt_param WHERE node_name='246') AND rgt<=(SELECT rgt FROM mptt_param WHERE node_name='246') ;
I don't know what the problem is, hope you can give me some advice

Related

Why using inner join itself is more faster than where

Here is my table:
CREATE TABLE `cerp_oms_order` (
`id` bigint NOT NULL,
`company_id` bigint NOT NULL,
`order_no` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`source_type` int NOT NULL,
`shop_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`outer_shop` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`origin_status` int NOT NULL,
`system_status` int NOT NULL,
`created_time` datetime DEFAULT NULL,
`paid_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`end_time` datetime DEFAULT NULL,
`modify_time` datetime DEFAULT NULL,
`delivery_deadline_time` datetime DEFAULT NULL,
`amount` text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`spu_kind` int NOT NULL,
`sku_kind` int NOT NULL,
`total_quantity` decimal(16,4) NOT NULL,
`buyer_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`outer_buyer_identifier` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
`tax_info` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`warehouse_owner` int DEFAULT NULL,
`warehouse_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`logistics_type` int NOT NULL,
`logistics_outer_info` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`delivery_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`delivery_no` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`wave_no` varchar(64) DEFAULT '',
`is_deleted` tinyint NOT NULL DEFAULT '0',
`backend_processing_type` tinyint NOT NULL,
`create_type` tinyint NOT NULL,
`is_hang_up` tinyint NOT NULL,
`hang_up_case_type` smallint DEFAULT NULL,
`hang_up_case_id` bigint DEFAULT NULL,
`rc_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`rm_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`vat` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`weight` decimal(16,4) NOT NULL DEFAULT '0.0000',
`volume` decimal(16,4) NOT NULL DEFAULT '0.0000',
`is_abnormal` tinyint NOT NULL DEFAULT '0',
`estimate_profit` decimal(16,4) NOT NULL DEFAULT '0.0000',
`business_man_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`business_man` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`currency` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`net_amount_summary` decimal(19,4) NOT NULL,
`domestic_amount` decimal(19,4) NOT NULL,
`secret_key` varchar(64) DEFAULT '',
`secretKey` varchar(255) DEFAULT NULL,
`sale_id` bigint DEFAULT NULL,
`total_refund_include_tax` decimal(16,4) NOT NULL DEFAULT '0.0000',
`total_refund_money` decimal(16,4) NOT NULL DEFAULT '0.0000',
`total_refund_tax` decimal(16,4) NOT NULL DEFAULT '0.0000',
`total_return_goods` decimal(16,2) NOT NULL DEFAULT '0.00',
PRIMARY KEY (`id`),
UNIQUE KEY `key_order_no` (`order_no`),
KEY `idx_order_company_id` (`company_id`,`created_time`),
KEY `IDX_RM_TIME` (`rm_time`),
KEY `IDX_IS_ABNORMAL` (`is_abnormal`),
KEY `cerp_oms_order_company_id_index` (`company_id`),
KEY `idx_order_company_status_deleted` (`company_id`,`is_deleted`,`system_status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='system order table'
explain select *
from cerp_oms_order
inner join (select id
from cerp_oms_order
where source_type = 43
order by created_time) as tmp using (id);
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
cerp_oms_order
null
ALL
PRIMARY
null
null
null
60787
10
Using where
1
SIMPLE
cerp_oms_order
null
eq_ref
PRIMARY
PRIMARY
8
cerp_oms_1.cerp_oms_order.id
1
100
null
using inner join execution: 7 ms, fetching: 109 ms
VS
explain
select *
from cerp_oms_order
where source_type = 43
order by created_time;
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
cerp_oms_order
null
ALL
null
null
null
null
60787
10
Using where; Using filesort
using simple where clause execution: 80 ms, fetching: 138 ms
I don't understand why use inner join can speed up my sql?
If you had
INDEX(source_type, created_time)
both formulations would run faster. And, I think, the second would be faster.
As for "why". Look at what the second does:
Scan the entire table ("table scan") looking for rows with source_type = 43.
For each matching row, gather all the columns (*) from the row, into a temp table.
Sort that temp table. Note that this is bulkier than with the subquery that hauls around only id.
Deliver the results.
With the INDEX I suggest:
Look in the index's B+Tree for all 'rows' with source_type = 43.
Since those rows are sorted by created_time, not "sort" pass will be needed now or later.
Each of those index rows will contain id. (That's the way InnoDB gets to the rest of the columns), so
Look up the desired rows in the data's B+Tree and pull out all (*) the columns.
Deliver them.
Note that the JOIN version works essentially the same, but, technically speaking, the ORDER BY may get lost. If it does get lost, you would need to add a second ORDER BY, thereby forcing a sort.
(Unrelated) Your "KEY cerp_oms_order_company_id_index (company_id)," can be dropped since there are two other indexes starting with company_id.
More on optimizing indexes: Index Cookbook

Mysql primary key association does not use index

I have a SQL problem.
When the user, organization, and organization are associated with the table, if the user status is used to filter the table, the index user_id cannot be used. If the condition is removed, the index user_id will be used.
Why is that?
MSYQL VERSION:5.7.32-log
Below is the specific SQL and table structure.
sql 1 :
SELECT DISTINCT USER
.user_id,
USER.NAME,
USER.nickname,
USER.position,
USER.first_line_id,
USER.second_line_id,
USER.org_id,
user.state
FROM
USER INNER JOIN user_org ON USER.user_id = user_org.user_id
INNER JOIN org ON user_org.org_id = org.id
WHERE
( org.end_time IS NULL OR org.end_time > NOW( ) )
AND USER.state = 1
AND ( full_id LIKE 'H_ROOT.00000001.00000002.50060182.50091585.50095679.50092012.10148706.50092333.10161139%' )
explain:user_id index not sufficient
sql2 :
SELECT DISTINCT USER
.user_id,
USER.NAME,
USER.nickname,
USER.position,
USER.first_line_id,
USER.second_line_id,
USER.org_id,
user.state
FROM
USER INNER JOIN user_org ON USER.user_id = user_org.user_id
INNER JOIN org ON user_org.org_id = org.id
WHERE
( org.end_time IS NULL OR org.end_time > NOW( ) )
-- AND USER.state = 1
AND ( full_id LIKE 'H_ROOT.00000001.00000002.50060182.50091585.50095679.50092012.10148706.50092333.10161139%' )
explain:user_id index sufficient
table count
USER:356007
ORG:142713
USER_ORG:353088
table schema
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `user_org`;
CREATE TABLE `user_org` (
`user_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`org_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`created_at` datetime(0) NULL DEFAULT NULL,
`updated_at` datetime(0) NULL DEFAULT NULL,
PRIMARY KEY (`user_id`, `org_id`) USING BTREE,
INDEX `org_id`(`org_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`user_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '工号',
`name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '姓名',
`email` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱',
`email_private` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '个人邮箱',
`mobile` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '手机号',
`position` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '岗位',
`state` tinyint(4) NOT NULL DEFAULT 1 COMMENT '状态(1:启用;0:禁用)',
`org_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '部门编码',
PRIMARY KEY (`user_id`) USING BTREE,
INDEX `user_email_index`(`email`) USING BTREE,
INDEX `user_mobile_index`(`mobile`) USING BTREE,
INDEX `user_name_index`(`name`) USING BTREE,
INDEX `user_org_id_index`(`org_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户表' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `org`;
CREATE TABLE `org` (
`id` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`name` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`parent_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`full_id` varchar(512) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`end_time` datetime(0) NULL DEFAULT NULL COMMENT '部门过期时间',
`created_at` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
`updated_at` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '更新时间',
`customer_code` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '',
`org_type` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '组织类别',
`state` tinyint(4) NULL DEFAULT NULL COMMENT ' 1 正常 2 停用\r\n冗余目前还是用endtime来识别有效性',
PRIMARY KEY (`id`) USING BTREE,
INDEX `org_full_id_index`(`full_id`(255)) USING BTREE,
INDEX `org_name_index`(`name`(255)) USING BTREE,
INDEX `org_parent_id_index`(`parent_id`) USING BTREE,
INDEX `end_time`(`end_time`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '组织表' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
STRAIGHT_JOIN not sufficient
STRAIGHT_JOIN not sufficien v2
FORCE INDEX not sufficient
FORCE INDEX not sufficient v2
What version of MySQL are you using? There have been Optimization and Index-limit changes that are relevant to your query and schema.
If you set end_time to some date in the distant future, you could avoid the OR by changing to simply end_time > NOW(). (OR used to be bad for performance.)
The indexes you have for the many-to-many table (user_org) are optimal.
Index "prefixing" (full_id(255)) is problematic. It can be eliminated in newer versions. INDEX(full_id) would let the query start with `full_id LIKE '...%' be much more usable.
Perhaps you should change to utf8mb4? It is needed for the more obscure Chinese characters, plus some Emoji.
This index may be picked by the Optimizer; suggest you add it:
USER: INDEX(state, user_id)
If you don't actually need user.name to be a full 256 characters, lower it to 255. That way you can eliminate the prefixing:
USER: INDEX(name)
See other options here: http://mysql.rjweb.org/doc.php/limits#767_limit_in_innodb_indexes

MySQL - Copy row to another table with foreign key

i want to copy 1 row from this table:
tableold
-----------
oid (primary key, auto_incremment)
name
age
detail
to this table:
tablenew
------------
nid (primary key, auto_incremment)
fid (foreign key to another table)
name
age
detail
With this SQL-Command:
INSERT INTO tablenew (tablenew.name, tablenew.age, tablenew.detail)
SELECT tableold.name, tableold.age, tableold.detail
FROM tableold
WHERE tableold.oid = 123;
But i get the error:
ERROR 1364 (HY000): Field 'fid' doesn't have a default value
Is there any possibility to manually add the foreign key 'fid' to the SQL-Command?
*Edit, i have added the CREATE table statements:
CREATE TABLE `othertable` (
`fid` int NOT NULL AUTO_INCREMENT,
`value1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`value2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`value3` longtext CHARACTER SET utf8 COLLATE utf8_general_ci,
`value4` longtext CHARACTER SET utf8 COLLATE utf8_general_ci,
PRIMARY KEY (`fid`),
) ENGINE=InnoDB AUTO_INCREMENT=66 DEFAULT CHARSET=utf8;
CREATE TABLE `tablenew` (
`nid` int NOT NULL AUTO_INCREMENT,
`fid` int NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`age` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`detail` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`sid`),
KEY `fid_fkey` (`fid`),
CONSTRAINT `fid_fkey` FOREIGN KEY (`fid`) REFERENCES `othertable` (`fid`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
CREATE TABLE `tableold` (
`oid` int NOT NULL AUTO_INCREMENT,
`name` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`age` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`detail` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`oid`)
) ENGINE=InnoDB AUTO_INCREMENT=157 DEFAULT CHARSET=utf8;
I have solved it:
INSERT INTO tablenew (tablenew.fid, tablenew.name, tablenew.age, tablenew.detail)
SELECT 'value', tableold.name, tableold.age, tableold.detail
FROM tableold
WHERE tableold.oid = value;

I get the error: incorrect table definition [duplicate]

This question already has answers here:
There can be only one auto column
(6 answers)
Closed 2 years ago.
Im using phpmyadmin for the first time and I get this error: incorrect table definition. there can be only one auto column and it must be defined as key. What am I doing wrong?
This is my code:
CREATE TABLE `database_reservering`.`formData` (
`nameTeacher` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`nameChild` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , `email` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`age` INT(11) NOT NULL ,
`date` DATE NOT NULL ,
`comment` VARCHAR(300) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`id` INT(30) UNSIGNED NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`nameTeacher`)
) ENGINE = InnoDB;
The actual error message is:
Incorrect table definition; there can be only one auto column and it must be defined as a key
The problem is that you have id as auto-increment, but the primary key is on nameTeacher. This is not allowed. You can change the statement to make id the primary key, and put a unique constraint on nameTeacher. This implements the same logic, but is valid MySQL syntax:
CREATE TABLE `formData` (
...
PRIMARY KEY (`id`),
UNIQUE (`nameTeacher`)
) ENGINE = InnoDB;
Demo on DB Fiddle
Change
PRIMARY KEY (nameTeacher)
to
PRIMARY KEY (id)
Full statement:
CREATE TABLE formData (
nameTeacher VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
nameChild VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
email VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
age INT(11) NOT NULL ,
`date` DATE NOT NULL ,
`comment` VARCHAR(300) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (id)) ENGINE = InnoDB;
What do you want to achieve when PK is set to the column other than AUTO_INCREMENT?
If you want to have separate independent autoincremented sequence for each nameTeacher value then alter the engine to MyISAM and define PK like (nameTeacher, id):
CREATE TABLE `formData` (
`nameTeacher` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`nameChild` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`email` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`age` INT(11) NOT NULL , `date` DATE NOT NULL ,
`comment` VARCHAR(300) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`id` INT(30) UNSIGNED NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`nameTeacher`, `id`)
) ENGINE = MyISAM;
fiddle
See Using AUTO_INCREMENT for details.

MySQL updates column to always NULL

I have two tables which basically one is a master table and the other one is a table for developers. From time to time I would copy review the record from the developer table and move it to the master table:
--SHOW CREATE TABLE DOCUMENT --
CREATE TABLE `document` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(1000) CHARACTER SET utf8 NOT NULL,
`title` varchar(1000) CHARACTER SET utf8 NOT NULL,
`content` longtext CHARACTER SET utf8,
`source_type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`date_crawled` datetime DEFAULT NULL,
`mime_type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`date_posted` datetime DEFAULT NULL,
`tier` int(11) DEFAULT NULL,
`html_content` longtext CHARACTER SET utf8,
`dev` varchar(255) CHARACTER SET utf8 NOT NULL,
`dev_document_id` int(11) NOT NULL,
`promote` int(11) DEFAULT NULL,
`domain` varchar(128) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`,`url`(500),`dev`,`dev_document_id`),
UNIQUE KEY `url` (`url`)
) ENGINE=InnoDB AUTO_INCREMENT=628871 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=COMPRESSED
--SHOW CREATE TABLE DOCUMENT_DEVELOPER--
CREATE TABLE `document_developer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(1000) CHARACTER SET utf8 NOT NULL,
`title` varchar(1000) CHARACTER SET utf8 DEFAULT NULL,
`content` longtext CHARACTER SET utf8,
`jurisdiction` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
`source_type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`date_crawled` datetime DEFAULT NULL,
`mime_type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`html_content` longtext CHARACTER SET utf8,
`date_posted` datetime DEFAULT NULL,
`tier` int(11) DEFAULT NULL,
`promote` int(11) NOT NULL,
`modified` tinyint(1) NOT NULL DEFAULT '0',
`date_modified` datetime DEFAULT NULL,
UNIQUE KEY `url_promote_UNIQUE` (`url`,`promote`),
KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=637067 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=COMPRESSED
/*!50100 PARTITION BY LIST (promote)
(PARTITION part0 VALUES IN (-5) ENGINE = InnoDB,
PARTITION part1 VALUES IN (-4) ENGINE = InnoDB,
PARTITION part2 VALUES IN (-3) ENGINE = InnoDB,
PARTITION part3 VALUES IN (-2) ENGINE = InnoDB,
PARTITION part4 VALUES IN (-1) ENGINE = InnoDB,
PARTITION part5 VALUES IN (0) ENGINE = InnoDB,
PARTITION part6 VALUES IN (1) ENGINE = InnoDB,
PARTITION part7 VALUES IN (2) ENGINE = InnoDB) */
The column of dev_document_id will be the id of record in document_developer once the copy is done.
However, when i try to run update as below:
UPDATE document as d INNER JOIN document_developer AS dd ON d.dev_document_id = dd.id
SET
d.content=dd.content
WHERE dd.modified='1' AND dd.promote='2';
it will always give me erroneous content value such as NULL NULL NULL ... Any help will be greatly appreciated.
Your query should be
UPDATE document as d INNER JOIN document_developer AS da ON
d.dev_document_id = da.id
SET
d.content=da.content
WHERE da.modified='1' AND da.promote='2';