请教索引优化问题
sqlserver吧
全部回复
仅看楼主
level 1
allen9046 楼主
我的数据库表device_data有字段id,deviceId,macid,uploadtime等字段,其中id是自增。
这个表现在数据在2千万左右。
有联合索引 idx_deviceId(deviceId,uploadtime)和
联合索引 idx_mac(macid,uploadtime)
目前出现了一个问题就是插入数据会出现超时
插入是批量插入,每批插入的数据行数不超过300条
我怀疑是索引的问题
所以我把 deviceid,macid,uploadtime分别独立建立索引
请教下这样是否有问题
2021年01月06日 09点01分 1
level 6
建议检查空间,估计是IO不够了。插入数据需要占用索引空间和表空间。
如果你确认报错只是超时而不是因为事务冲突而被KILL或者触发器拦截的话,90%是空间问题。
另外有没有报错的截图?
至于:
deviceid,macid,uploadtime 三个分建独立索引,估计如果是空间问题的话估计也会报错。
而且性能上联合索引再同时查询的时候会用到,反而独立索引的话系统估计默认走 uploadtime字段的索引,由于uploadtime字段是有序自增,而且从你两个索引看错uploadtime是查询必选项。
这样deviceid,macid 两个独立索引就是多余的,反而增加了insert时候的负担。
2021年01月06日 09点01分 2
你好,其实三个字段都是必须的,这张表我有两个查询,一个是根据deviceid和time查询结果,一个是根据macid和time查询记录,所以这也是我建立两个两个联合索引的问题,磁盘空间是够的有400g的空余
2021年01月07日 01点01分
,还有个问题请教下,就是我这个两个联合索引都有time,这样和三个独立索引想必是不是time索引要存储两遍了 占用了更多的空间
2021年01月07日 01点01分
@allen9046 time索引在两个复合索引的时候已经自动添加了,然后在单独索引的时候又需要另外添加。这样变成time有3个索引。系统在默认查询time的时候优先会走单独索引,在复合查询的时候根据字段吻合性走不同复合索引。
2021年01月07日 02点01分
level 1
allen9046 楼主
这是报错
01:00:58.721 5 Y 1 System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
---> System.ComponentModel.Win32Exception (258): 等待的操作过时。
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
ClientConnectionId:ff3c03b5-84c4-4838-ae25-9d6eae9a583d
2021年01月07日 01点01分 3
level 6
你的报错并不是SQL server端的报错。建议你提取SQL语句在查询分析器上执行看看。
你是用C#执行的insert语句吧,看看是不是你的数据框架有BUG,遇到特殊字符识别不了。组成的SQL出错了比较好。跟索引无关。
2021年01月07日 02点01分 4
解决了 是对吗接口慢查询引起的死锁,感觉大佬
2021年01月07日 07点01分
1