doushan6692 2017-03-13 06:59
浏览 102

如何忽略myIsam插入时自动增加的主ID忽略?

How to ignore automatically increased primary id on myIsam insert ignore? How to solve this problem? This is increased primary id rapidly. How to solve this

MY TABLE STRUCTURE

    | dates_tbl | CREATE TABLE `dates_tbl` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `date_raw` int(8) unsigned NOT NULL DEFAULT '0',
  `date` date NOT NULL DEFAULT '0000-00-00',
  `day` tinyint(2) unsigned NOT NULL DEFAULT '0',
  `week` tinyint(2) unsigned NOT NULL DEFAULT '0',
  `month` tinyint(2) unsigned NOT NULL DEFAULT '0',
  `year` year(4) NOT NULL DEFAULT '0000',
  `created_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `modified_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `date_raw_unique` (`date_raw`),
  KEY `date_raw` (`date_raw`),
  KEY `month_year` (`month`,`year`),
  KEY `year` (`year`)
) ENGINE=InnoDB AUTO_INCREMENT=21628 DEFAULT CHARSET=latin1 |

INSERT QUERY

    $q = "INSERT IGNORE INTO dates_tbl(date_raw,date,day,week,month,year,created_on) 
values $dynamic_value";

RESULT

mysql> select id from dates_tbl limit 10;
+-------+
| id    |
+-------+
| 19657 |
| 19681 |
| 19729 |
| 19777 |
| 19825 |
| 19873 |
| 19884 |
| 19913 |
| 19960 |
| 20007 |
+-------+
10 rows in set (0.01 sec)
  • 写回答

1条回答 默认 最新

  • dongzong2017 2017-03-15 02:28
    关注
    • The table is InnoDB, why do you mention MyISAM?
    • Get rid of id, it probably serves no purpose. Instead, promote date_raw to be the PRIMARY KEY.
    • Avoid redundant indexes -- a PRIMARY KEY is a UNIQUE KEY is a KEY.
    • Usually it is better to keep year+month+day in a single DATE column, then pick it apart when needed.
    • Do you actually use created_on and modified_on? Or is that an artifact of some 3rd party software?
    • It is more efficient to have an index on a DATE, then use something like

    this:

    WHERE ymd >= '2010-03-01'
      AND ymd  < '2010-03-01' + INTERVAL 1 MONTH
    

    With those changes, you have eliminated half the columns and most of the indexes. And the INSERT IGNORE will stop giving you troubles.

    If you keep the AUTO_INCREMENT, then let's see some more of the logic -- need to see why IGNORE is taking effect. The solution may involve some of the other code in addition to the INSERT.

    Of this is part of a star schema in Data Warehousing, then I will rant about how it is bad to normalize "continuous" values, such as DATE. At that point, the whole table vanishes. And the code will run faster!

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能