如果需要的话,MySQL 存储过程要 INSERT DELAYED 但先 CREATE TABLE 吗?
我计划在 MyISAM
表中执行大量 INSERT DELAYED
操作。但这些表可能还不存在。假设每天都会有一个新表。
因此,与其检测客户端中是否存在表并创建然后重试插入,这似乎是存储过程(“函数”)的一个好例子。
这可能吗?它会是什么样子? 这种方法有什么缺点吗?
I'm planning on doing a lot of INSERT DELAYED
into MyISAM
tables. But the tables might not exist yet. Let's say e.g. a new table will exist for each day.
So instead of detecting absence of table in my client and creating then retrying the insert, it seems like a good case for a stored procedure ("function").
Is this possible, and what would it look like?
Are there any downsides to this approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每次插入时检查表是否存在似乎是浪费时间。如果您需要日常表(在我看来,它们违背了数据库中通常存在的大部分逻辑,因此我会尽量避免使用它们):
另一种方法是合并表(参见http://dev.mysql.com/ doc/refman/5.0/en/merge-storage-engine.html),每日 cronjob 创建“今天”表和使用 alter table 语句更新合并表的表列表。
It seems a waste of time to check with every single insert whether the table exist. If you need day by day tables (in my opinion they defy much of the logic usually found in databases, so I try to avoid them, I'd):
Another approach would be a MERGE table (see http://dev.mysql.com/doc/refman/5.0/en/merge-storage-engine.html), with a daily cronjob creating the 'today' table & updating the tablelist for the merge table with an alter table statement.
创建一个每天晚上午夜之前运行的计划事件并创建第二天的日志表不是一个好主意吗?这样您就可以始终依赖在发生任何
INSERT
之前创建的表?Wouldn't it be an idea to create a scheduled event that runs every night before midnight, and create the log-table for the next day. That way you could always rely on the table beeing created before any
INSERT
would occur?你可以制定一个好的计划来实现同样的目标。
例如,如果您有一个每天都会创建的表“Logged_users”,您可以将一列附加到用户表中,或者创建一个包含 ID_USER, DAY 列的额外表(这将是一个更好的选择)。然后,您可以编写一个 CRON 作业来删除过去几天的每一行,并在每天中午 12:00 执行该作业。
You may make a good scheme to accomplish the same.
For example, if you have a table "Logged_users", which will be created every day, you could just append a column to the users table, or make an extra table with ID_USER, DAY columns (that would be a better choice). Then, you may program a CRON job to delete every rows of past days, and execute that job every day at 12:00 am.