I have a script that read 500,000 row data from excel file and then insert into MSSQL database;
how can decrease insert time?
my code is like below :
cnxn = pyodbc.connect('DRIVER={SQL Server};Server=DESKTOP\MS2019;\
Database=203-Temp;Port=1433;User ID=sa;Password=1234;TrustServerCertificate=True')
cnxn.autocommit = False
cursor = cnxn.cursor()
data_df = pd.read_excel('a.xlsx').fillna(value=b'0')
data_dict = data_df.to_dict("records")
for row in tqdm(data_dict, desc="Inserting Data Into DataBase"):
c1= row['col1']
c2= row['col2']
c3= row['col3']
c4= row['col4']
c5= row['col5']
c6= row['col6']
c7= row['col7']
c8= row['col8']
c9= row['col9']
c10= row['col10']
cursor.execute(
"INSERT INTO Temp (UID, Name, ShName, GID, GLink,"
"GName,GAbout,PUID,PUnID,Date)"
" values (?,?,?,?,?,?,?,?,?,?)", c1, c2, c3, c4, c5, c6, c7, c8, c9, c10)
cursor.commit()
I used method DataFrame.to_dict() to regular datas.
I think use "BULK INSERT " or using method curser.executemany() can help me.
what do you offer me?
how to use this method?