
我好想知道你在问什么,系统当前时间表示为now(); 假如表test中有字段id ,createtime
插入数据的sql语句为: insert into test (id,createtime) values('11',now());
在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题
下面介绍SQL Server支持的两种批量数据插入方法:Bulk和表值参数(Table-Valued Parameters)。
bulk方法主要思想是通过在客户端把数据都缓存在Table中,然后利用SqlBulkCopy一次性把Table中的数据插入到数据库
代码如下:
public static void BulkToDB(DataTable dt)
{
SqlConnection sqlConn = new SqlConnection(
ConfigurationManagerConnectionStrings["ConnStr"]ConnectionString);
SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConn);
bulkCopyDestinationTableName = "BulkTestTable";
bulkCopyBatchSize = dtRowsCount;
try
{
sqlConnOpen();
if (dt != null && dtRowsCount != 0)
bulkCopyWriteToServer(dt);
}
catch (Exception ex)
{
throw ex;
}
finally
{
sqlConnClose();
if (bulkCopy != null)
bulkCopyClose();
}
}
public static DataTable GetTableSchema()
{
DataTable dt = new DataTable();
dtColumnsAddRange(new DataColumn[]{
new DataColumn("Id",typeof(int)),
new DataColumn("UserName",typeof(string)),
new DataColumn("Pwd",typeof(string))});
return dt;
}
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
for (int multiply = 0; multiply < 10; multiply++)
{
DataTable dt = BulkGetTableSchema();
for (int count = multiply 100000; count < (multiply + 1) 100000; count++)
{
DataRow r = dtNewRow();
r[0] = count;
r[1] = stringFormat("User-{0}", count multiply);
r[2] = stringFormat("Pwd-{0}", count multiply);
dtRowsAdd(r);
}
swStart();
BulkBulkToDB(dt);
swStop();
ConsoleWriteLine(stringFormat("Elapsed Time is {0} Milliseconds", swElapsedMilliseconds));
}
ConsoleReadLine();
}
前提:在做insert数据之前,如果是非生产环境,请将表的索引和约束去掉,待insert完成后再建索引和约束。
insert into tab1 select from tab2; commit;
这是最基础的insert语句,我们把tab2表中的数据insert到tab1表中。根据经验,千万级的数据可在1小时内完成。但是该方法产生的arch会非常快,需要关注归档的产生量,及时启动备份软件,避免arch目录撑爆。
alter table tab1 nologging;
insert /+ append / into tab1 select from tab2;
commit; alter table tab1 logging;
该方法会使得产生arch大大减少,并且在一定程度上提高时间,根据经验,千万级的数据可在45分钟内完成。但是请注意,该方法适合单进程的串行方式,如果当有多个进程同时运行时,后发起的进程会有enqueue的等待。注意此方法千万不能dataguard上用,不过要是在database已经force logging那也是没有问题的。
insert into tab1 select /+ parallel / from tab2; commit;
对于select之后的语句是全表扫描的情况,我们可以加parallel的hint来提高其并发,这里需要注意的是最大并发度受到初始化参数parallel_max_servers的限制,并发的进程可以通过v$px_session查看,或者ps -ef |grep ora_p查看。
alter session enable parallel dml;
insert /+ parallel / into tab1 select from tab2; commit;
其他方法:
并发的insert,尚未比较和方法2哪个效率更高(偶估计是方法2快),有测试过的朋友欢迎补充。
insert into tab1 select from tab2 partition (p1);
insert into tab1 select from tab2 partition (p2);
insert into tab1 select from tab2 partition (p3);
insert into tab1 select from tab2 partition (p4);
对于分区表可以利用tab1进行多个进程的并发insert,分区越多,可以启动的进程越多。我曾经试过insert 26亿行记录的一个表,8个分区,8个进程,如果用方法2,单个进程完成可能要40分钟,但是由于是有8个分区8个进程,后发进程有enqueue,所以因此需要的时间为40分钟×8;但是如果用方法5,虽然单个进程需要110分钟,但是由于能够并发进程执行,所以总共需要的时间就约为110分钟了。
DECLARE TYPE dtarray IS TABLE OF VARCHAR2(20)
INDEX BY BINARY_INTEGER;
v_col1 dtarray; v_col2 dtarray; v_col3 dtarray;
BEGIN SELECT col1, col2, col3 BULK COLLECT INTO v_col1, v_col2, v_col3
FROM tab2;
FORALL i IN 1 v_col1COUNT insert into tab1
WHERE tab1col1 = v_col1;
END;
用批量绑定(bulk binding)的方式。当循环执行一个绑定变量的sql语句时候,在PL/SQL 和SQL引擎(engines)中,会发生大量的上下文切换(context switches)。使用bulk binding,能将数据批量的从plsql引擎传到sql引擎,从而减少上下文切换过程,提升效率。该方法比较适合于在线处理,不必停机。
sqlplus -s user/pwd< runlogtxt set copycommit 2;
set arraysize 5000;
copy from user/pwd@sid - to user/pwd@sid - insert tab1 using
select from tab2; exit EOF
用copy的方法进行插入,注意此处insert没有into关键字。该方法的好处是可以设置copycommit和arrarysize来一起控制commit的频率,上面的方法是每10000行commit一次。
DataTable dt = new DataTable();
Book _o = null;
for (int i = 0; i < 3; i++)
{
_o = new Book();
_oBookName = dtRows[i]["bookName"]ToString();
_oBookID = dtRows[i]["bookID"]ToString();
InsertBookSpeakFor(_o);
}
你用上面的看看什么结果 还有问题 那就要看你对象保存清空的地方了
一般后缀为DT的是数据库的文件。 *** 作系统文件 一般是ISO文件(镜像文件),还有GHO(一种系统恢复文件),对用户没什么意义sql server的文件后缀只有3种:mdf ndf ldf bak分别为主数据文件,次数据文件,日志文件,备份文件
在手机中,System\Data目录里存放着很多ini,dat,cdr等文件,它们都是程序所保存设置的数据文件,请不要随意删除和修改它们,除非你知道它们的确切意义,在这个目录里有一些重要文件System\Data\Calendr记录着日程表数据System\Data\Contactscdb和System\Data\CntModelini记录着名片夹数据(正常情况下你不可以复制Contactscdb文件进行保存名片数据)System\data\Notepaddat保存着记事本中的内容System\data\backgroundimagembm是桌面墙纸文件System\data\Applicationsdat是程序数据文件System\data\wapstore目录中保存着wap的相关数据
这个一般如果你删除了是找不回来了。你可以试试数据恢复
DateTimeNowToShortTimeString()
DateTime dt = DateTimeNow;
dtToString();//2005-11-5 13:21:25
dtToFileTime()ToString();//127756416859912816
dtToFileTimeUtc()ToString();//127756704859912816
dtToLocalTime()ToString();//2005-11-5 21:21:25
dtToLongDateString()ToString();//2005年11月5日
dtToLongTimeString()ToString();//13:21:25
dtToOADate()ToString();//386615565508218
dtToShortDateString()ToString();//2005-11-5
dtToShortTimeString()ToString();//13:21
dtToUniversalTime()ToString();//2005-11-5 5:21:25
dtYearToString();//2005
dtDateToString();//2005-11-5 0:00:00
dtDayOfWeekToString();//Saturday
dtDayOfYearToString();//309
dtHourToString();//13
dtMillisecondToString();//441
dtMinuteToString();//30
dtMonthToString();//11
dtSecondToString();//28
dtTicksToString();//632667942284412864
dtTimeOfDayToString();//13:30:284412864
dtToString();//2005-11-5 13:47:04
dtAddYears(1)ToString();//2006-11-5 13:47:04
dtAddDays(11)ToString();//2005-11-6 16:11:04
dtAddHours(11)ToString();//2005-11-5 14:53:04
dtAddMilliseconds(11)ToString();//2005-11-5 13:47:04
dtAddMonths(1)ToString();//2005-12-5 13:47:04
dtAddSeconds(11)ToString();//2005-11-5 13:47:05
dtAddMinutes(11)ToString();//2005-11-5 13:48:10
dtAddTicks(1000)ToString();//2005-11-5 13:47:04
dtCompareTo(dt)ToString();//0
dtAdd()ToString();//问号为一个时间段
dtEquals("2005-11-6 16:11:04")ToString();//False
dtEquals(dt)ToString();//True
dtGetHashCode()ToString();//1474088234
dtGetType()ToString();//SystemDateTime
dtGetTypeCode()ToString();//DateTime
dtGetDateTimeFormats('s')[0]ToString();//2005-11-05T14:06:25
dtGetDateTimeFormats('t')[0]ToString();//14:06
dtGetDateTimeFormats('y')[0]ToString();//2005年11月
dtGetDateTimeFormats('D')[0]ToString();//2005年11月5日
dtGetDateTimeFormats('D')[1]ToString();//2005 11 05
dtGetDateTimeFormats('D')[2]ToString();//星期六 2005 11 05
dtGetDateTimeFormats('D')[3]ToString();//星期六 2005年11月5日
dtGetDateTimeFormats('M')[0]ToString();//11月5日
dtGetDateTimeFormats('f')[0]ToString();//2005年11月5日 14:06
dtGetDateTimeFormats('g')[0]ToString();//2005-11-5 14:06
dtGetDateTimeFormats('r')[0]ToString();//Sat, 05 Nov 2005 14:06:25 GMT
stringFormat("{0:d}",dt);//2005-11-5
stringFormat("{0}",dt);//2005年11月5日
stringFormat("{0:f}",dt);//2005年11月5日 14:23
stringFormat("{0:F}",dt);//2005年11月5日 14:23:23
stringFormat("{0:g}",dt);//2005-11-5 14:23
stringFormat("{0:G}",dt);//2005-11-5 14:23:23
stringFormat("{0:M}",dt);//11月5日
stringFormat("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
stringFormat("{0:s}",dt);//2005-11-05T14:23:23
stringFormat("{0:t}",dt);//14:23
stringFormat("{0:T}",dt);//14:23:23
stringFormat("{0:u}",dt);//2005-11-05 14:23:23Z
stringFormat("{0:U}",dt);//2005年11月5日 6:23:23
stringFormat("{0:Y}",dt);//2005年11月
stringFormat("{0}",dt);//2005-11-5 14:23:23
stringFormat("{0:yyyyMMddHHmmssffff}",dt);
计算2个日期之间的天数差
-----------------------------------------------
DateTime dt1 = ConvertDateTime("2007-8-1");
DateTime dt2 = ConvertDateTime("2007-8-15");
TimeSpan span = dt2Subtract(dt1);
int dayDiff = spanDays + 1;
计算某年某月的天数
-----------------------------------------------
int days = DateTimeDaysInMonth(2007, 8);
days = 31;
给日期增加一天、减少一天
-----------------------------------------------
DateTime dt =DateTimeNow;
dtAddDays(1); //增加一天
dtAddDays(-1);//减少一天
其它年份方法类似
Oracle SQL里转换日期函数
-----------------------------------------------
to_date("2007-6-6",'YYYY-MM-DD");
to_date("2007/6/6",'yyyy/mm/dd");
如下一组数据,如何查找表里包含9月份的记录:
CGGC_STRATDATE CGGC_ENDDATE
=========================================
2007-8-4 2007-9-5
2007-9-5 2007-9-20
2007-9-22 2007-10-5
SELECT FROM TABLE
(TO_DATE('2007/9/1','yyyy/mm/dd') BETWEEN CGGC_STRATDATE
AND CGGC_ENDDATE OR CGGC_STRATDATE >=TO_DATE('2007/9/1','yyyy/mm/dd')
AND CGGC_ENDDATE<=TO_DATE('2007/9/30','yyyy/mm/dd') "
OR TO_DATE('2007/9/30','yyyy/mm/dd') BETWEEN CGGC_STRATDATE
AND CGGC_ENDDATE) ORDER BY CGGC_STRATDATE ASC
以上就是关于Mysql问题,要向数据表中插入数据其中有一个字段是时间,它是系统时间,那么这个字段要不要插入数据呀全部的内容,包括:Mysql问题,要向数据表中插入数据其中有一个字段是时间,它是系统时间,那么这个字段要不要插入数据呀、像数据库一次性插入10w条数据,怎么插入效率快啊!、Oracle中插入数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)