使用 a 和 b 从同一个表中进行选择。根据 a 中的条件从 b 中选择并插入到新表中。查询需要 30 + 分钟才能运行
drop table if exists feedlot.deaths;
CREATE TABLE `feedlot`.`deaths` (
`recno` INT(30) NOT NULL ,
`calfrecno` VARCHAR(50) NULL ,
`lotrecno` VARCHAR (20) NULL ,
`treatdate` DATE NULL ,
`deaths_tothispoint` INT(3) NULL,
`distinct_deaths_tothispoint` INT(3) NULL
);
insert into deaths
( recno,
calfrecno,
lotrecno,
treatdate,
deaths_tothispoint,
distinct_deaths_tothispoint
)
SELECT
a.recno,
a.calfrecno,
a.lotrecno,
a.treatdate,
count(b.didnotfinish),
count(distinct(b.calfrecno))
FROM feedlot.brdcalves_test a ,feedlot.brdcalves_test b
JOIN brdcalves_test ON a.lotrecno = b.lotrecno
WHERE b.lotrecno = a.lotrecno
AND b.didnotfinish = 1
AND b.treatdate < a.treatdate
GROUP BY a.recno;
mysql> select count(*) from brdcalves_test;
+----------+
| count(*) |
+----------+
| 6314459 |
+----------+
1 row in set (3.16 sec)
服务器是 Win 7 48GB RAM Intel Xenon 2.26Ghz(2 个处理器)
mysql> show variables like '%innodb%';
+------------------------------------------+------------------------+
| Variable_name | Value |
+------------------------------------------+------------------------+
| ignore_builtin_innodb | OFF |
| innodb_adaptive_flushing | ON |
| innodb_adaptive_flushing_lwm | 10 |
| innodb_adaptive_hash_index | ON |
| innodb_adaptive_max_sleep_delay | 150000 |
| innodb_additional_mem_pool_size | 33554432 |
| innodb_api_bk_commit_interval | 5 |
| innodb_api_disable_rowlock | OFF |
| innodb_api_enable_binlog | OFF |
| innodb_api_enable_mdl | OFF |
| innodb_api_trx_level | 0 |
| innodb_autoextend_increment | 1000 |
| innodb_autoinc_lock_mode | 1 |
| innodb_buffer_pool_dump_at_shutdown | OFF |
| innodb_buffer_pool_dump_now | OFF |
| innodb_buffer_pool_filename | ib_buffer_pool |
| innodb_buffer_pool_instances | 10 |
| innodb_buffer_pool_load_abort | OFF |
| innodb_buffer_pool_load_at_startup | OFF |
| innodb_buffer_pool_load_now | OFF |
| innodb_buffer_pool_size | 12884901888 |
| innodb_change_buffer_max_size | 25 |
| innodb_change_buffering | all |
| innodb_checksum_algorithm | crc32 |
| innodb_checksums | ON |
| innodb_cmp_per_index_enabled | OFF |
| innodb_commit_concurrency | 0 |
| innodb_compression_failure_threshold_pct | 5 |
| innodb_compression_level | 6 |
| innodb_compression_pad_pct_max | 50 |
| innodb_concurrency_tickets | 5000 |
| innodb_data_file_path | ibdata1:12M:autoextend |
| innodb_data_home_dir | |
| innodb_disable_sort_file_cache | OFF |
| innodb_doublewrite | ON |
| innodb_fast_shutdown | 1 |
| innodb_file_format | Antelope |
| innodb_file_format_check | ON |
| innodb_file_format_max | Antelope |
| innodb_file_per_table | ON |
| innodb_flush_log_at_timeout | 1 |
| innodb_flush_log_at_trx_commit | 1 |
| innodb_flush_method | |
| innodb_flush_neighbors | 1 |
| innodb_flushing_avg_loops | 30 |
| innodb_force_load_corrupted | OFF |
| innodb_force_recovery | 0 |
| innodb_ft_aux_table | |
| innodb_ft_cache_size | 8000000 |
| innodb_ft_enable_diag_print | OFF |
| innodb_ft_enable_stopword | ON |
| innodb_ft_max_token_size | 84 |
| innodb_ft_min_token_size | 3 |
| innodb_ft_num_word_optimize | 2000 |
| innodb_ft_server_stopword_table | |
| innodb_ft_sort_pll_degree | 2 |
| innodb_ft_user_stopword_table | |
| innodb_io_capacity | 200 |
| innodb_io_capacity_max | 2000 |
| innodb_large_prefix | OFF |
| innodb_lock_wait_timeout | 50 |
| innodb_locks_unsafe_for_binlog | OFF |
| innodb_log_buffer_size | 16777216 |
| innodb_log_compressed_pages | ON |
| innodb_log_file_size | 268435456 |
| innodb_log_files_in_group | 2 |
| innodb_log_group_home_dir | .\ |
| innodb_lru_scan_depth | 1024 |
| innodb_max_dirty_pages_pct | 75 |
| innodb_max_dirty_pages_pct_lwm | 0 |
| innodb_max_purge_lag | 0 |
| innodb_max_purge_lag_delay | 0 |
| innodb_mirrored_log_groups | 1 |
| innodb_monitor_disable | |
| innodb_monitor_enable | |
| innodb_monitor_reset | |
| innodb_monitor_reset_all | |
| innodb_old_blocks_pct | 37 |
| innodb_old_blocks_time | 1000 |
| innodb_online_alter_log_max_size | 134217728 |
| innodb_open_files | 300 |
| innodb_optimize_fulltext_only | OFF |
| innodb_page_size | 16384 |
| innodb_print_all_deadlocks | OFF |
| innodb_purge_batch_size | 300 |
| innodb_purge_threads | 1 |
| innodb_random_read_ahead | OFF |
| innodb_read_ahead_threshold | 56 |
| innodb_read_io_threads | 4 |
| innodb_read_only | OFF |
| innodb_replication_delay | 0 |
| innodb_rollback_on_timeout | OFF |
| innodb_rollback_segments | 128 |
| innodb_sort_buffer_size | 1048576 |
| innodb_spin_wait_delay | 6 |
| innodb_stats_auto_recalc | ON |
| innodb_stats_method | nulls_equal |
| innodb_stats_on_metadata | OFF |
| innodb_stats_persistent | ON |
| innodb_stats_persistent_sample_pages | 20 |
| innodb_stats_sample_pages | 8 |
| innodb_stats_transient_sample_pages | 8 |
| innodb_strict_mode | OFF |
| innodb_support_xa | ON |
| innodb_sync_array_size | 1 |
| innodb_sync_spin_loops | 30 |
| innodb_table_locks | ON |
| innodb_thread_concurrency | 17 |
| innodb_thread_sleep_delay | 0 |
| innodb_undo_directory | . |
| innodb_undo_logs | 128 |
| innodb_undo_tablespaces | 0 |
| innodb_use_native_aio | ON |
| innodb_use_sys_malloc | ON |
| innodb_version | 5.6.11 |
| innodb_write_io_threads | 4 |
+------------------------------------------+------------------------+
SELECT
a.recno,
a.calfrecno,
a.lotrecno,
a.treatdate,
count(b.didnotfinish),
count(distinct(b.calfrecno))
FROM feedlot.brdcalves_test a ,feedlot.brdcalves_test b
JOIN brdcalves_test ON a.lotrecno = b.lotrecno
WHERE b.lotrecno = a.lotrecno
AND b.didnotfinish = 1
AND b.treatdate < a.treatdate
GROUP BY a.recno;
CREATE TABLE `brdcalves_test` (
`recno` int(30) NOT NULL DEFAULT '0',
`calfrecno` varchar(50) DEFAULT NULL,
`fyydlotno` varchar(50) DEFAULT NULL,
`lotrecno` varchar(20) DEFAULT NULL,
`FY` varchar(10) DEFAULT NULL,
`YDNO` varchar(10) DEFAULT NULL,
`tagno` varchar(50) DEFAULT NULL,
`lotno` varchar(50) DEFAULT NULL,
`Indate` date DEFAULT NULL,
`Pen` varchar(10) DEFAULT NULL,
`headin` int(11) DEFAULT NULL,
`sex_new` varchar(10) DEFAULT NULL,
`totinwt` int(11) DEFAULT NULL,
`avginwt` int(11) DEFAULT NULL,
`totpaywt` int(11) DEFAULT NULL,
`avgpaywt` int(11) DEFAULT NULL,
`Risk` varchar(10) DEFAULT NULL,
`Treatdate` date DEFAULT NULL,
`eventno` int(4) DEFAULT NULL,
`Diag` varchar(45) DEFAULT NULL,
`dxcode` varchar(45) DEFAULT NULL,
`Antimicrobial` int(2) DEFAULT NULL
`deaddate` date DEFAULT '1111-11-11',
`didnotfinish` int(2) DEFAULT '0',
`treatdof_new` int(11) DEFAULT NULL
`Wt` int(10) DEFAULT NULL,
`Temp` decimal(5,2) DEFAULT NULL,
`Meda` varchar(10) DEFAULT NULL,
`Medb` varchar(10) DEFAULT NULL,
`Medc` varchar(10) DEFAULT NULL,
`Medd` varchar(10) DEFAULT NULL,
`Mede` varchar(10) DEFAULT NULL,
`Medf` varchar(10) DEFAULT NULL,
`overallclass` varchar(45) DEFAULT NULL,
`nsaidyn` int(2) DEFAULT '0' COMMENT 'Indicates if this calf received an NSAID on this treatment\\n1 = YES\\n0 = NO',
`classconcat` varchar(70) DEFAULT NULL,
`Massdate` date DEFAULT NULL,
`Procdate` date DEFAULT NULL,
`Dieddx` varchar(45) DEFAULT NULL,
`dieddxcode` varchar(45) DEFAULT NULL,
`abxtreatno` int(2) DEFAULT NULL
`abxtreatno_runningtotal` int(2) DEFAULT NULL
`treatnobrdabx` int(2) DEFAULT NULL
`treatnobrdabx_runningtotal` int(2) DEFAULT NULL,
`abxtx_after_brdabxtx_yn` int(1) DEFAULT NULL,
`pulled_after_brdabxtx_yn` int(1) DEFAULT NULL,
`txfailurep3` int(3) DEFAULT NULL,
`txfailurep4` int(3) DEFAULT NULL,
`txfailureP1` int(3) DEFAULT NULL
`txfailurep2` int(3) DEFAULT NULL
`Antimica_new` int(2) DEFAULT NULL
`Antimicb_new` int(2) DEFAULT NULL COMMENT '1 if Medb is an antimicrobial otherwise 0',
`Antimicc_new` int(2) DEFAULT NULL COMMENT '1 if Medac is an antimicrobial otherwise 0',
`Antimicd_new` int(2) DEFAULT NULL COMMENT '1 if Medd is an antimicrobial otherwise 0',
`Antimice_new` int(2) DEFAULT NULL COMMENT '1 if Mede is an antimicrobial otherwise 0',
`Antimicf_new` int(2) DEFAULT NULL COMMENT '1 if Medf is an antimicrobial otherwise 0',
`Antimic_concat` varchar(30) DEFAULT NULL
`brdabxyn` int(2) DEFAULT '0'
`sumbrdabxyn` int(2) DEFAULT NULL
`eventspriortobrdyn` int(2) DEFAULT '0'
`nbreventspriortobrd` int(2) DEFAULT '0'
`numbprevpulls` int(1) DEFAULT NULL
`arrivalmonth` int(3) DEFAULT NULL
`arrivalquarter` int(3) DEFAULT NULL
`arrivalyear` int(5) DEFAULT NULL
`pulldayofweek` varchar(20) DEFAULT NULL
`pullweekdayYN` int(2) DEFAULT NULL
`pullonmondayYN` int(2) DEFAULT '0'
`pullmonth` int(3) DEFAULT NULL
`pullquarter` int(3) DEFAULT NULL
`pullyear` int(3) DEFAULT NULL,
`1sttxsuccessrate_p1` decimal(5,2) DEFAULT NULL
`1sttxsuccessrate_p2` decimal(5,2) DEFAULT NULL
`1sttxsuccessrate_p3` decimal(5,2) DEFAULT NULL,
`1sttxsuccessrate_p4` decimal(5,2) DEFAULT NULL,
`propbrdcasestothispoint` decimal(5,2) DEFAULT NULL COMMENT 'brdcasestothispoint/headin)*100',
`distinct_brdcasestothispoint` int(3) DEFAULT NULL COMMENT 'number of animals treated for BRD prior to this event.',
`propdistcasestothispoint` decimal(5,2) DEFAULT NULL COMMENT '(distinct_brdcasestothispoint/headin)*100',
`brdcases_includetoday` int(3) DEFAULT NULL,
`distinct_brdcases_includetoday` int(3) DEFAULT NULL,
`propbrdcases_includetoday` decimal(5,2) DEFAULT NULL,
`propdistcases_includetoday` decimal(5,2) DEFAULT NULL,
`brdcases103andover` int(3) DEFAULT NULL,
`propbrdcases103andover` decimal(5,2) DEFAULT NULL,
`distinct_brdcases103andover` int(3) DEFAULT NULL,
`propdistcases103andover` decimal(5,2) DEFAULT NULL,
`brdcases1035andover` int(3) DEFAULT NULL,
`propbrdcases1035andover` decimal(5,2) DEFAULT NULL,
`distinct_brdcases1035andover` int(3) DEFAULT NULL,
`propdistcases1035andover` decimal(5,2) DEFAULT NULL,
`brdcases104andover` int(3) DEFAULT NULL,
`propbrdcases104andover` decimal(5,2) DEFAULT NULL,
`distinct_brdcases104andover` int(3) DEFAULT NULL,
`brdcases2prevdays` int(3) DEFAULT NULL,
`propbrdcases2prevdays` decimal(5,2) DEFAULT NULL,
`distinct_brdcases2prevdays` int(3) DEFAULT NULL,
`propdistcases2prevdays` decimal(5,2) DEFAULT NULL,
`propdistcases104andover` decimal(5,2) DEFAULT NULL,
`brdcases2prevdays_104` int(3) DEFAULT NULL,
`propbrdcases2prevdays104` decimal(5,2) DEFAULT NULL,
`distinct_brdcases2prevdays_104` int(3) DEFAULT NULL,
`propdistcases2prevdays104` decimal(5,2) DEFAULT NULL,
`brdcases2prevdays_1035` int(3) DEFAULT NULL,
`propbrdcases2prevdays1035` decimal(5,2) DEFAULT NULL,
`distinct_brdcases2prevdays_1035` int(3) DEFAULT NULL,
`propdistcases2prevdays1035` decimal(5,2) DEFAULT NULL,
`brdcases2prevdays_103` int(3) DEFAULT NULL,
`propbrdcases2prevdays103` decimal(5,2) DEFAULT NULL,
`distinct_brdcases2prevdays_103` int(3) DEFAULT NULL,
`propdistcases2prevdays103` decimal(5,2) DEFAULT NULL,
`brdcases3prevdays` int(3) DEFAULT NULL,
`distinct_brdcases3prevdays` int(3) DEFAULT NULL,
`propbrdcases3prevdays` decimal(5,2) DEFAULT NULL,
`propdistcases3prevdays` decimal(5,2) DEFAULT NULL,
`brdcases3prevdays_103` int(3) DEFAULT NULL,
`distinct_brdcases3prevdays_103` int(3) DEFAULT NULL,
`propbrdcases3prevdays103` decimal(5,2) DEFAULT NULL,
`propdistcases3prevdays103` decimal(5,2) DEFAULT NULL,
`brdcases3prevdays_1035` int(3) DEFAULT NULL,
`distinct_brdcases3prevdays_1035` int(3) DEFAULT NULL,
`propbrdcases3prevdays1035` decimal(5,2) DEFAULT NULL,
`propdistcases3prevdays1035` decimal(5,2) DEFAULT NULL,
`brdcases3prevdays_104` int(3) DEFAULT NULL,
`distinct_brdcases3prevdays_104` int(3) DEFAULT NULL,
`propbrdcases3prevdays104` decimal(5,2) DEFAULT NULL,
`propdistcases3prevdays104` decimal(5,2) DEFAULT NULL,
`brdcases5prevdays` int(3) DEFAULT NULL,
`propbrdcases5prevdays` decimal(5,2) DEFAULT NULL,
`distinct_brdcases5prevdays` int(3) DEFAULT NULL,
`propdistcases5prevdays` decimal(5,2) DEFAULT NULL,
`brdcases5prevdays_103` int(3) DEFAULT NULL,
`propbrdcases5prevdays_103` decimal(5,2) DEFAULT NULL,
`distinct_brdcases5prevdays_103` int(3) DEFAULT NULL,
`propdistcases5prevdays_103` decimal(5,2) DEFAULT NULL,
`brdcases5prevdays_1035` int(3) DEFAULT NULL,
`propbrdcases5prevdays_1035` decimal(5,2) DEFAULT NULL,
`distinct_brdcases5prevdays_1035` int(3) DEFAULT NULL,
`propdistcases5prevdays_1035` decimal(5,2) DEFAULT NULL,
`brdcases5prevdays_104` int(3) DEFAULT NULL,
`propbrdcases5prevdays_104` decimal(5,2) DEFAULT NULL,
`distinct_brdcases5prevdays_104` int(3) DEFAULT NULL,
`propdistcases5prevdays_104` decimal(5,2) DEFAULT NULL,
`brdcases10prevdays` int(3) DEFAULT NULL,
`propbrdcases10prevdays` decimal(5,2) DEFAULT NULL,
`distinct_brdcases10prevdays` int(3) DEFAULT NULL,
`propdistcases10prevdays` decimal(5,2) DEFAULT NULL,
`brdcases10prevdays_103` int(3) DEFAULT NULL,
`propbrdcases10prevdays_103` decimal(5,2) DEFAULT NULL,
`distinct_brdcases10prevdays_103` int(3) DEFAULT NULL,
`propdistcases10prevdays_103` decimal(5,2) DEFAULT NULL,
`brdcases10prevdays_1035` int(3) DEFAULT NULL,
`propbrdcases10prevdays_1035` decimal(5,2) DEFAULT NULL,
`distinct_brdcases10prevdays_1035` int(3) DEFAULT NULL,
`propdistcases10prevdays_1035` decimal(5,2) DEFAULT NULL,
`brdcases10prevdays_104` int(3) DEFAULT NULL,
`propbrdcases10prevdays_104` decimal(5,2) DEFAULT NULL,
`distinct_brdcases10prevdays_104` int(3) DEFAULT NULL,
`propdistcases10prevdays_104` decimal(5,2) DEFAULT NULL,
`deathstothispoint` int(3) DEFAULT NULL,
`propdeathstothispoint` decimal(5,2) DEFAULT NULL,
`deathsprev2days` int(3) DEFAULT '0',
`propdeaths2prevdays` decimal(5,2) DEFAULT '0.00',
`deathsprev3days` int(3) DEFAULT '0',
`propdeaths3prevdays` decimal(5,2) DEFAULT '0.00',
`deathsprev5days` int(3) DEFAULT '0',
`propdeaths5prevdays` decimal(5,2) DEFAULT '0.00',
`deathsprev10days` int(3) DEFAULT '0',
`propdeaths10prevdays` decimal(5,2) DEFAULT '0.00',
KEY `recno_indx` (`recno`),
KEY `lottreatdidnot_indx` (`lotrecno`,`Treatdate`,`didnotfinish`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
非常感谢任何帮助。