So, I have two tables filled with for the most part very similar data, for example, a row in each table may have the same first name, last name, and address, but have a different phone number or email address based on the most recently available data which was updated in a separate excel worksheet (out of my hands, my job is just to merge this data into our latest database which they plan to use from here on out, not the excel sheet). I just need a good way to merge these tables with the same column names without doing it manually (about 24,000+) records.
Here is the Create Table Syntax for both tables:
CREATE TABLE `UsersUpdated` (
`FULLNME` longtext,
`LSTNME` varchar(23) CHARACTER SET utf8 DEFAULT NULL,
`FSTNME` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`MID` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`SUFF` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`STAT` varchar(2) CHARACTER SET utf8 DEFAULT NULL,
`PTY` varchar(3) CHARACTER SET utf8 DEFAULT NULL,
`PH` bigint(20) DEFAULT NULL,
`ALTPH` bigint(20) DEFAULT NULL,
`DOB` datetime DEFAULT NULL,
`REGDTE` datetime DEFAULT NULL,
`ADDR` text,
`ST` int(11) DEFAULT NULL,
`STNME` varchar(19) CHARACTER SET utf8 DEFAULT NULL,
`APT` varchar(7) CHARACTER SET utf8 DEFAULT NULL,
`TWN` varchar(6) CHARACTER SET utf8 DEFAULT NULL,
`ZIP` int(11) DEFAULT NULL,
`W` int(11) DEFAULT NULL,
`d` int(11) DEFAULT NULL,
`G17` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P17` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`G16` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P16` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`G15` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P15` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`G14` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P14` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`SIGN` tinyint(1) NOT NULL DEFAULT '0',
`SUPP` tinyint(1) NOT NULL DEFAULT '0',
`NOTES` longtext,
`LTR` tinyint(1) DEFAULT NULL,
`REGISTERED` tinyint(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `Users` (
`FULLNME` longtext,
`LSTNME` varchar(23) CHARACTER SET utf8 DEFAULT NULL,
`FSTNME` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`MID` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`SUFF` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`STAT` varchar(2) CHARACTER SET utf8 DEFAULT NULL,
`PTY` varchar(3) CHARACTER SET utf8 DEFAULT NULL,
`PH` bigint(20) DEFAULT NULL,
`ALTPH` bigint(20) DEFAULT NULL,
`DOB` datetime DEFAULT NULL,
`REGDTE` datetime DEFAULT NULL,
`ADDR` text,
`ST` int(11) DEFAULT NULL,
`STNME` varchar(19) CHARACTER SET utf8 DEFAULT NULL,
`APT` varchar(7) CHARACTER SET utf8 DEFAULT NULL,
`TWN` varchar(6) CHARACTER SET utf8 DEFAULT NULL,
`ZIP` int(11) DEFAULT NULL,
`W` int(11) DEFAULT NULL,
`d` int(11) DEFAULT NULL,
`G17` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P17` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`G16` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P16` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`G15` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P15` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`G14` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`P14` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
`SIGN` tinyint(1) NOT NULL DEFAULT '0',
`SUPP` tinyint(1) NOT NULL DEFAULT '0',
`NOTES` longtext,
`LTR` tinyint(1) DEFAULT NULL,
`REGISTERED` tinyint(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
As you can see, they are basically the same exact tables, I just need to merge them correctly.
Perhaps this is helpful.
update Users
set ADDR = (
select ADDR from UsersUpdated uu
where uu.FULLNME = Users.FULLNME and uu.DOB = Users.DOB
), STNME = (
select STNME from UsersUpdated uu
where uu.FULLNME = Users.FULLNME and uu.DOB = Users.DOB
) ...
;
You can add all the columns to a single update. Depending on the size of the database it might just be as easy to do them individually.
Many platforms allow for a from clause with update that permits a join and a shorter query but it can be problematic. This way you will get errors if any of the subqueries don't return just a single value.
I would modify the Users table to add a UNIQUE key on the fields that should be the same (presumably FSTNME, LSTNME and ADDR from your description but perhaps you might use some other columns e.g. DOB as suggested by #shawnt00), then INSERT the data from UsersUpdated into Users using an ON DUPLICATE KEY UPDATE clause to copy updated data into Users where the user already exists in that table. This query will also work when there are users in UsersUpdated who are not already in Users. So,
ALTER TABLE Users ADD UNIQUE KEY NameAddr (FSTNME, LSTNME, ADDR);
INSERT INTO Users
SELECT * FROM UsersUpdated
ON DUPLICATE KEY UPDATE
FULLNME=VALUES(FULLNME),
LSTNME=VALUES(LSTNME),
FSTNME=VALUKES(FSTNME),
...
LTR=VALUES(LTR),
REGISTERED=VALUES(REGISTERED);
Related
Today I've created a sql backup of a table using SQLyog Community Edition. Then I tried to run the created query against an up to date MySQL 8 managed database on Digital Ocean.
The query:
CREATE TABLE tblUsers (
id bigint unsigned NOT NULL AUTO_INCREMENT,
subscriberId bigint unsigned DEFAULT NULL,
user varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
password varchar(64) DEFAULT NULL,
created timestamp NULL DEFAULT NULL,
modified timestamp NULL DEFAULT NULL,
status varchar(16) DEFAULT NULL,
privilege varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
firstName varchar(64) DEFAULT NULL,
lastName varchar(64) DEFAULT NULL,
address1 varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
city varchar(32) DEFAULT NULL,
state char(2) DEFAULT NULL,
postalCode char(6) DEFAULT NULL,
phone varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
fax varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
email varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
notes varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
payRate varchar(16) DEFAULT NULL,
pay decimal(8,2) unsigned DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
The warning:
Warning Code : 1681
UNSIGNED for decimal and floating point data types is deprecated and support
for it will be removed in a future release.
The research:
https://dev.mysql.com/doc/refman/8.0/en/numeric-type-attributes.html
As of MySQL 8.0.17, AUTO_INCREMENT support is deprecated for FLOAT and
DOUBLE columns; you should expect it to be removed in a future version
of MySQL. Consider removing the AUTO_INCREMENT attribute from such
columns, or convert them to an integer type.
The question:
I'm not certain how to interpret what I'm reading. Is it really telling me to remove my primary key auto increments? Or is it telling me the pay field should no longer be define the way you see above? Or that AUTO_INCREMENT cannot exist together with unsigned?
This warning is not about your auto-increment column id. It's about the decimal column pay.
Change this:
pay decimal(8,2) unsigned DEFAULT NULL,
To this:
pay decimal(8,2) DEFAULT NULL,
The related excerpt from the manual page you linked to is the following. It tells you what to do if you want to restrict the column to nonnegative values:
As of MySQL 8.0.17, the UNSIGNED attribute is deprecated for columns of type FLOAT, DOUBLE, and DECIMAL (and any synonyms) and you should expect support for it to be removed in a future version of MySQL. Consider using a simple CHECK constraint instead for such columns.
In your case it would look like this:
pay decimal(8,2) DEFAULT NULL,
CHECK (pay >= 0),
Updated example:
id field remains unchanged
subscriberId field loses UNSIGNED and gains CHECK
pay field loses UNSIGNED and gains CHECK
CREATE TABLE tblUsers (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`subscriberId` BIGINT DEFAULT NULL CHECK (subscriberId >= 0),
`user` VARCHAR(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`password` VARCHAR(64) DEFAULT NULL,
`created` TIMESTAMP NULL DEFAULT NULL,
`modified` TIMESTAMP NULL DEFAULT NULL,
`status` VARCHAR(16) DEFAULT NULL,
`privilege` VARCHAR(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`firstName` VARCHAR(64) DEFAULT NULL,
`lastName` VARCHAR(64) DEFAULT NULL,
`address1` VARCHAR(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`city` VARCHAR(32) DEFAULT NULL,
`state` CHAR(2) DEFAULT NULL,
`postalCode` CHAR(6) DEFAULT NULL,
`phone` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`fax` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`email` VARCHAR(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`notes` VARCHAR(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`payRate` VARCHAR(16) DEFAULT NULL,
`pay` DECIMAL(8,2) DEFAULT NULL CHECK (pay >= 0),
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=465000 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Update / Answer
To fix this issue, I added the keyword BINARY to each side of the comparison operator. This generated the expected results. Also note that I set the variable to empty string. Setting this to NULL did not resolve the issue.
SET #previousstate = '';
SELECT
if(**BINARY** #previousstate != **BINARY** frm.fi_details_temp.PortfolioCode,
End Update / Answer
This is somewhat related to MYSQL Stored Procedure - Concat Row Value. That thread was based on Mysql 5.7.
The following statement worked under MySQL 5.7. I am in the process of trying to get it to work in MySql 8. I had to update the SET statement to 'SET #previousstate = NULL;' otherwise I get "Illegal mix of collations (utf8mb4_0900_ai_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for operation '<>'". This error was way above my understanding. I am just a marketing guy working in a small firm. So that is why I set it to NULL.
With that said, after updating the SET statement the result set returns but the if() statement is not working like it did under 5.7. The very first row returned should have the first column of data marked with '*' at the beginning and end, and as the data in the column changes it should mark it too.
So it should look like this:
*brownfdn*
brownfdn
*brownfmi*
brownfmi
But I am getting this:
brownfdn
brownfdn
brownfmi
brownfmi
Do you have any thoughts about what I am doing wrong? I appreciate your help!! Getting to MySql 8 has been a struggle.
5.7
CREATE table fi_details_temp AS (
SELECT
frm.fi_details.PortfolioCode,
frm.fi_details.MaturityDate_Final
From
frm.fi_details
Where
frm.fi_details.manager = 'Bartz' And
frm.fi_details.PortfolioCode Like ('Crown%')
Group By
frm.fi_details.PortfolioCode,
frm.fi_details.MaturityDate_Final
Order By
frm.fi_details.PortfolioCode;
SET #previousstate = '';
SELECT
if(#previousstate != frm.fi_details_temp.PortfolioCode, concat('*',#previousstate:= frm.fi_details_temp.PortfolioCode,'*'), frm.fi_details_temp.PortfolioCode) as 'Portfolio Code',
frm.fi_details_temp.MaturityDate_Final as 'Maturity Date'
From frm.fi_details_temp;
DROP TABLE IF EXISTS fi_details_temp;
END
*** Create Statements - the above was a trimmed down version ***
CREATE TABLE `fi_details` (
`id` int NOT NULL AUTO_INCREMENT,
`RecordDate` datetime DEFAULT CURRENT_TIMESTAMP,
`PortfolioCode` varchar(8) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`manager` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`RunDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`RunDate_Final` date DEFAULT NULL,
`AsOfDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`AsOfDate_Final` date DEFAULT NULL,
`Symbol` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`Coupon` double DEFAULT NULL,
`MaturityDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`MaturityDate_Final` date DEFAULT NULL,
`ParValue` double DEFAULT NULL,
`MarketValue` double DEFAULT NULL,
`AdjustedUnitCost` double DEFAULT NULL,
`TotalAdjustedCost` double DEFAULT NULL,
`SPRating` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`MoodyRating` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`YieldCost` double DEFAULT NULL,
`YieldMarket` double DEFAULT NULL,
`YieldCall` double DEFAULT NULL,
`YieldPut` double DEFAULT NULL,
`YieldWorst` double DEFAULT NULL,
`WorstDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`WorstDate_Final` date DEFAULT NULL,
`CallDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`CallDate_Final` date DEFAULT NULL,
`CallPrice` double DEFAULT NULL,
`PutDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`PutDate_Final` date DEFAULT NULL,
`PutPrice` double DEFAULT NULL,
`FirstCouponDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`FirstCouponDate_Final` date DEFAULT NULL,
`LastCouponDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`LastCouponDate_Final` date DEFAULT NULL,
`Freq` int DEFAULT NULL,
`BondCalendarCode` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`SecTypeCode` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`Security` varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_fi_details_MaturityDate_Final` (`MaturityDate_Final`),
KEY `idx_fi_details_PortfolioCode` (`PortfolioCode`),
KEY `idx_fi_details_PortfolioCode_MaturityDate_Final` (`PortfolioCode`,`MaturityDate_Final`),
KEY `idx_fi_details_manager_MaturityDate_Final` (`manager`,`MaturityDate_Final`),
KEY `idx_fi_details_MaturityDate_Final_manager` (`MaturityDate_Final`,`manager`)
) ENGINE=InnoDB AUTO_INCREMENT=234850 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE DEFINER=`root`#`localhost` PROCEDURE `fi_details_Worst_Date_Hartz_Brown`()
BEGIN
DROP TABLE IF EXISTS fi_details_temp;
CREATE table fi_details_temp AS (
SELECT
frm.fi_details.PortfolioCode,
frm.fi_details.MaturityDate_Final,
frm.fi_details.YieldWorst,
frm.fi_details.WorstDate_Final,
frm.fi_details.YieldMarket,
frm.fi_details.Symbol,
frm.fi_details.Security,
frm.fi_details.Coupon,
frm.fi_details.ParValue,
frm.fi_details.MarketValue,
frm.fi_details.TotalAdjustedCost,
frm.fi_details.AdjustedUnitCost,
frm.fi_details.YieldCost,
frm.fi_details.YieldCall,
frm.fi_details.YieldPut,
frm.fi_details.CallDate_Final
From
frm.fi_details
Where
frm.fi_details.manager = 'Hartz' And
frm.fi_details.PortfolioCode Like ('Brown%')
Group By
frm.fi_details.PortfolioCode,
frm.fi_details.MaturityDate_Final,
frm.fi_details.YieldWorst,
frm.fi_details.WorstDate_Final,
frm.fi_details.YieldMarket,
frm.fi_details.Symbol,
frm.fi_details.Security,
frm.fi_details.Coupon,
frm.fi_details.ParValue,
frm.fi_details.MarketValue,
frm.fi_details.TotalAdjustedCost,
frm.fi_details.AdjustedUnitCost,
frm.fi_details.YieldCost,
frm.fi_details.YieldCall,
frm.fi_details.YieldPut,
frm.fi_details.CallDate_Final
Order By
frm.fi_details.PortfolioCode,
frm.fi_details.WorstDate_Final );
SET #previousstate = NULL;
SELECT
if(#previousstate != frm.fi_details_temp.PortfolioCode, concat('*',#previousstate:= frm.fi_details_temp.PortfolioCode,'*'), frm.fi_details_temp.PortfolioCode) as 'Portfolio Code',
frm.fi_details_temp.MaturityDate_Final as 'Maturity Date',
frm.fi_details_temp.YieldWorst as 'Worst Yield',
frm.fi_details_temp.WorstDate_Final as 'Worst Date',
frm.fi_details_temp.YieldMarket as 'Yield to Market',
frm.fi_details_temp.Symbol,
frm.fi_details_temp.Security as 'Description',
frm.fi_details_temp.Coupon,
frm.fi_details_temp.ParValue as 'Par Value',
frm.fi_details_temp.MarketValue as 'Market Value xAI',
frm.fi_details_temp.TotalAdjustedCost as 'Adjusted Cost',
frm.fi_details_temp.AdjustedUnitCost as 'Unit Adjusted Cost',
frm.fi_details_temp.YieldCost as 'Yield to Cost',
frm.fi_details_temp.YieldCall as 'Yield to Call',
frm.fi_details_temp.YieldPut as 'Yield to Put',
frm.fi_details_temp.CallDate_Final as 'Next Call Date'
From frm.fi_details_temp;
DROP TABLE IF EXISTS fi_details_temp;
END
I tried to query by join 2 tables giao_vien and lop_mon.
The following is my tables structure:
CREATE TABLE `lop_mon` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`key_index` varchar(250) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`khoi_key_index` varchar(250) DEFAULT NULL,
`lop_key_index` varchar(250) DEFAULT NULL,
`gv_key_index` varchar(250) DEFAULT NULL,
`mon_key_index` varchar(250) DEFAULT NULL,
`ma_so` varchar(50) DEFAULT NULL,
`ma_phong` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`nam_hoc` int(11) NOT NULL,
`ma_truong` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`ma_khoi` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`ma_lop` varchar(50) NOT NULL,
`ma_giao_vien` varchar(50) NOT NULL,
`ma_mon_hoc` varchar(50) DEFAULT NULL,
`hoc_ky` tinyint(4) NOT NULL,
`guid` varchar(250) DEFAULT NULL,
`processid_monhoc` int(11) DEFAULT NULL,
`processid_giaovien` int(11) DEFAULT NULL,
`processid_canbo` int(11) DEFAULT NULL,
`source_data` varchar(50) DEFAULT NULL,
`ten_gv_chat_ph` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`ma_giao_vien_bgd` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`ma_lop_bgd` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`ma_mon_hoc_bgd` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`gv_guid` char(36) DEFAULT NULL,
`truong_key_index` varchar(255) DEFAULT NULL,
`create_date` datetime DEFAULT NULL,
`updatedAt` datetime DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `key_UNIQUE` (`key_index`),
UNIQUE KEY `unique_lop_mon_gv_keyindex_hoc_ky` (`lop_key_index`,`gv_key_index`,`mon_key_index`,`hoc_ky`) USING BTREE,
KEY `lopmon_keyindex` (`mon_key_index`),
KEY `lopmon_idx_mamonhoc` (`ma_mon_hoc`),
KEY `idx_gv_key_index` (`gv_key_index`),
KEY `lopmon_idx_lop_key_index` (`lop_key_index`),
KEY `lopmon_idx_key_index` (`khoi_key_index`),
KEY `lopmon_idx_ma_truong` (`ma_truong`),
KEY `lopmon_idx_nam_hoc` (`nam_hoc`),
KEY `lopmon_idx_ma_khoi` (`ma_khoi`),
KEY `lopmon_idx_hoc_ky` (`hoc_ky`),
KEY `lopmon_idx_key_lop_mon` (`lop_key_index`,`mon_key_index`),
KEY `lopmon_idx_key_lop_gv` (`lop_key_index`,`gv_key_index`),
KEY `lopmon_idx_key_mon_gv` (`mon_key_index`,`gv_key_index`),
KEY `lopmon_idx_key_lop_mon_gv` (`lop_key_index`,`mon_key_index`,`gv_key_index`),
KEY `lopmon_idx_key_truong_lop_mon_gv` (`ma_truong`,`lop_key_index`,`mon_key_index`,`gv_key_index`),
KEY `lop_mon__index_gv_guid` (`gv_guid`),
KEY `lopmon_idx_truong_key_index` (`truong_key_index`)
) ENGINE=MyISAM AUTO_INCREMENT=23142297 DEFAULT CHARSET=latin1
My Query:
select * from giao_vien inner join lop_mon on lop_mon.gv_key_index = giao_vien.key_index
where giao_vien.key_index = 'shcm_2019_01645344018'
When I explain it:
MySQL does not recognize the key:
KEY `idx_gv_key_index` (`gv_key_index`)
I check another query:
SELECT * FROM enetviet.lop_mon where gv_key_index = 'shcm_2019_01645344018';
The key idx_gv_key_index is still work. I don't know what is the problem.
I wish to retrieve around 200 million profiles using PDO.
It is a partitioned table, so it does not have a primary key, it's sorted with INNODBs internal primary key.
I need to retrieve the data in internal sort order for performance, so using SORT BY some_id and to continue from there would not work for performance reasons.
Using LIMIT OFFSET would not work at all, OFFSET 100 million or more would take ages, the RAM is by far not enough to take all the terrabyte of table data into cache.
I would need something that fetches a chunk of rows, remembers the internal position and continues with next call.
Update
I was digging a bit and the problem seems familiar, it was solved in Oracle by having ROWID and ROWNUM which refer to the internal id.
It seems we have such an id in innodb as well but it's not queryable ?!
That sounds like a significant disadvantage of mysql for performant query.
Table def:
CREATE TABLE `members` (
`id` int(11) NOT NULL DEFAULT 0,
`intern_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`first_name` varchar(48) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`last_name` varchar(48) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`middle_name` varchar(48) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`l` varchar(196) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`i` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`ex` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`country_code` varchar(4) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`country_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`state_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`city_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`edu` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`nc` smallint(6) DEFAULT NULL,
`nj` smallint(6) DEFAULT NULL,
`jt` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`cn` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`email_address` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`fpe` tinyint(4) DEFAULT NULL COMMENT '1/0',
`pbu` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`cii` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`unmatched_facts` varchar(2048) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`dt_snapshot` datetime DEFAULT NULL,
`change_small` tinyint(4) DEFAULT NULL,
`change_significant` tinyint(4) DEFAULT NULL,
`jta` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`cna` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`cnia` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`ut_created` int(11) DEFAULT NULL ,
`reserve_int_2` int(11) DEFAULT NULL,
`reserve_vc1` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`reserve_vc2` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`reserve_vc_3` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
KEY `id` (`id`),
KEY `intern_id` (`intern_id`),
KEY `state_name` (`state_name`),
KEY `city_name` (`city_name`),
KEY `i` (`i`),
KEY `country_name` (`country_name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
PARTITION BY KEY (`country_name`)
PARTITIONS 30
We have a MySQL table hosted on an Amazon AWS server, which is some times very very slow while querying. I'm considering restructuring the table with better indexing and columns data types.
Here is the create table structure :
SHOW CREATE TABLE export_users;
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| exportusers | CREATE TABLE `exportusers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`UAccountID` varchar(50) CHARACTER SET utf8 NOT NULL,
`ApplyURL` text,
`CityName` varchar(64) CHARACTER SET utf8 DEFAULT NULL,
`ClassList` text,
`UCompanyID` varchar(20) DEFAULT NULL,
`UContactCompany` varchar(64) CHARACTER SET utf8 DEFAULT NULL,
`UContactEmail` varchar(512) CHARACTER SET utf8 DEFAULT NULL,
`UContactFax` varchar(32) CHARACTER SET utf8 DEFAULT NULL,
`UContactName` varchar(64) CHARACTER SET utf8 DEFAULT NULL,
`UContactPhone` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`CountryName` char(2) CHARACTER SET utf8 NOT NULL,
`DateCreated` datetime NOT NULL,
`DateModified` datetime NOT NULL,
`DateSysCreated` datetime NOT NULL,
`DateSysModified` datetime NOT NULL,
`DegreeCode` varchar(200) DEFAULT NULL,
`DegreeCodeDecoded` varchar(512) CHARACTER SET utf8 DEFAULT NULL,
`DisplayCity` varchar(64) CHARACTER SET utf8 DEFAULT NULL,
`DisplayuserID` varchar(32) CHARACTER SET utf8 DEFAULT NULL,
`ExperienceCode` varchar(200) DEFAULT NULL,
`ExperienceCodeDecoded` text,
`ExternalKey` varchar(32) CHARACTER SET utf8 DEFAULT NULL,
`GeoUSZip5` varchar(5) DEFAULT NULL,
`HostSite` char(2) CHARACTER SET utf8 NOT NULL,
`IndustryCode` varchar(128) CHARACTER SET utf8 DEFAULT NULL,
`IndustryCodeDecoded` text,
`IsBOFuser` tinyint(1) DEFAULT NULL,
`IsDiversityuser` tinyint(1) DEFAULT NULL,
`userID` varchar(20) NOT NULL,
`userDesc` text,
`userFunctionCode` text,
`userFunctionCodeDecoded` text,
`userReq` text,
`userSkinDID` varchar(20) DEFAULT NULL,
`userTitle` varchar(128) CHARACTER SET utf8 DEFAULT NULL,
`userType` varchar(512) CHARACTER SET utf8 DEFAULT NULL,
`userTypeDesc` text,
`userTypeCodeDecoded` text,
`Latitude` decimal(10,5) DEFAULT NULL,
`Longitude` decimal(10,5) DEFAULT NULL,
`Location` varchar(70) CHARACTER SET utf8 DEFAULT NULL,
`ManagementCode` varchar(20) CHARACTER SET utf8 DEFAULT NULL,
`MaximumExp` int(11) DEFAULT NULL,
`MinimunExp` int(11) DEFAULT NULL,
`Onet` varchar(10) DEFAULT NULL,
`OnetTitle` text,
`BeginDate` datetime NOT NULL,
`EndDate` datetime NOT NULL,
`PayBaseH` decimal(10,2) DEFAULT NULL,
`PayBaseL` decimal(10,2) DEFAULT NULL,
`PayBonus` decimal(10,2) DEFAULT NULL,
`PayComm` decimal(10,2) DEFAULT NULL,
`PayOther` varchar(64) CHARACTER SET utf8 DEFAULT NULL,
`PayPer` varchar(8) DEFAULT NULL,
`PayType` char(3) DEFAULT NULL,
`PostalCode` varchar(10) DEFAULT NULL,
`PostingPath` varchar(20) NOT NULL,
`Relocate` tinyint(1) DEFAULT NULL,
`RelocateOptions` varchar(5) DEFAULT NULL,
`ScreenerID` varchar(20) DEFAULT NULL,
`SiteID` varchar(1024) CHARACTER SET utf8 DEFAULT NULL,
`SliceList` text,
`StateName` char(30) CHARACTER SET utf8 DEFAULT NULL,
`Status` varchar(50) NOT NULL,
`TextPay` varchar(40) CHARACTER SET utf8 DEFAULT NULL,
`TotalPay` decimal(10,2) DEFAULT NULL,
`TownName` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`TravelCode` varchar(200) DEFAULT NULL,
`TravelCodeDecoded` text,
`UpgradeList` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `index_jobs_on_UAccountID` (`UAccountID`),
KEY `index_jobs_on_CountryName` (`CountryName`),
KEY `index_jobs_on_DateCreated` (`DateCreated`),
KEY `index_jobs_on_DateModified` (`DateModified`),
KEY `index_jobs_on_DateSysCreated` (`DateSysCreated`),
KEY `index_jobs_on_DateSysModified` (`DateSysModified`),
KEY `index_jobs_on_HostSite` (`HostSite`),
KEY `index_jobs_on_userID` (`userID`),
KEY `index_jobs_on_BeginDate` (`BeginDate`),
KEY `index_jobs_on_EndDate` (`EndDate`),
KEY `index_jobs_on_PostingPath` (`PostingPath`),
KEY `index_jobs_on_Status` (`Status`)
) ENGINE=InnoDB AUTO_INCREMENT=7907436 DEFAULT CHARSET=utf8mb4 |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.27 sec)
Consider Normalization of your schema. Breaking up that one very large entity into much smaller related entities will have the greatest impact on your querying speed.
Normalization involves decomposing a table into less redundant (and smaller) tables but without losing information; defining foreign keys in the old table referencing the primary keys of the new ones. The objective is to isolate data so that additions, deletions, and modifications of an attribute can be made in just one table and then propagated through the rest of the database using the defined foreign keys.