简体   繁体   English

SELECT *不返回所有行,除非我ORDER BY id DESC

[英]SELECT * Not returning all rows, unless I ORDER BY id DESC

Application presented with "Sequence Contains More Than One Entity" error. 应用程序显示“序列包含多个实体”错误。 Knowing this is usually the result of a .SingleOrDefault() command in Linq, I started investigating. 知道这通常是Linq中.SingleOrDefault()命令的结果,我开始调查。 I can verify that the production server has many instances of duplicate keywords, so that's where I begin. 我可以验证生产服务器有多个重复关键字的实例,这就是我开始的地方。

I have the following Keyword table: 我有以下关键字表:

  • id INT (NOT NULL, PRIMARY KEY) id INT(NOT NULL,PRIMARY KEY)
  • text NVARCHAR(512) 文字NVARCHAR(512)
  • active INT 有效的INT

active is just a way to "enable/disable" data if the need strikes me. 如果需要打击我,活动只是一种“启用/禁用”数据的方法。 I'm using LINQ to SQL and have the following method implemented: 我正在使用LINQ to SQL并实现以下方法:

public Keyword GetKeyword(String keywordText)
{
    return db.Keywords.SingleOrDefault(k => (k.text.ToUpper() == keywordText.ToUpper()));
}

The idea is that I attach keywords through an association table so that multiple objects can reference the same keyword. 我的想法是通过关联表附加关键字,以便多个对象可以引用相同的关键字。 There should be no duplicate text entries within the Keyword table. 关键字表中不应有重复的文本条目。 This is not enforced on the database, but rather through code. 这不是在数据库上强制执行,而是通过代码强制执行。 This might not be best practice, but that is the least of my problems at the moment. 这可能不是最佳做法,但这是我目前遇到的最少问题。 So when I create my object, I do this: 所以当我创建我的对象时,我这样做:

Keyword keyword = GetKeyword(keywordText)
if(keyword == null)
{
    keyword = new Keyword();
    keyword.text = keywordText;
    keyword.active = Globals.ACTIVE;
    db.Keywords.InsertOnSubmit(keyword);
}
KeywordReference reference = new KeywordReference();
reference.keywordId = keyword.id;
myObject.KeywordReferences.Add(reference);
db.SubmitChanges();

this code is actually paraphrased, I use a repository pattern, so all relevant code would be much longer. 这个代码实际上是释义的,我使用了一个存储库模式,所以所有相关的代码都会更长。 However, I can assure you the code is working as intended, as I've extensively tested it. 但是,我可以向您保证代码正在按预期工作,因为我已对其进行了广泛测试。 The problem seems to be happening on the database level. 问题似乎发生在数据库级别。

So I run a few tests and manual queries on my test database and find that the keyword I passed in my tests is not in the database, yet if I do a JOIN, I see that it is. 所以我在我的测试数据库上运行了一些测试和手动查询,发现我在测试中传递的关键字不在数据库中,但是如果我做了JOIN,我看到它是。 So I dig a little deeper and opt to manually scan through the whole list: 所以我深入挖掘并选择手动扫描整个列表:

SELECT * FROM Keyword

return 737 results. 返回737结果。 I decided I didn't want to look through them all, so I ORDER BY text and get 737 results. 我决定不想全部查看它们,所以我按文字排序并获得737结果。 I look for the word I recently added, and it does not show up in the list. 我寻找我最近添加的单词,它没有显示在列表中。

Confused, I do a lookup for all keywordIds associated with the object I recently worked on and see a few with ids over 1,000 (the id is set to autoincrement by 1). 困惑,我查找与我最近工作的对象关联的所有keywordIds,并查看一些id超过1,000(id设置为自动增量1)。 Knowing that I never actually delete a keyword (hence the "Active" column) so all ids from 1 to at least those numbers over 1,000 should all be present, I do another query: 知道我从来没有真正删除一个关键字(因此是“活动”列)所以从1到至少那些超​​过1,000的数字的所有ID都应该存在,我做另一个查询:

SELECT * FROM Keyword ORDER BY id

returns 737 results, max ID stops at 737. So I try: 返回737结果,最大ID停在737.所以我尝试:

SELECT * FROM Keyword ORDER BY id DESC

returns 1308 rows 返回1308行

I've seen disparities like this before if there is no primary key or no unique identifier, but I have confirmed that the id column is, in fact, both. 如果没有主键或没有唯一标识符,我之前看到过这样的差异,但我确认id列实际上都是。 I'm not really sure where to go from here, and I now have the additional problem of about 4,000+ keywords on production that are duplicate, and several more objects that reference different instances of each. 我不确定从哪里开始,我现在还有另外一个问题,即生产中大约有4,000多个关键字是重复的,还有几个引用不同实例的对象。

If you somehow can get all your data then your table is fine, but index is corrupted. 如果你以某种方式获得所有数据,那么你的表很好,但索引已损坏。

Try to rebuild index on your table: 尝试在表上重建索引:

ALTER INDEX ALL ON Your.Table
REBUILD WITH (FILLFACTOR = 80, SORT_IN_TEMPDB = ON, STATISTICS_NORECOMPUTE = ON);

In rare situations you can see such errors if your hard drive fails to write data due to power loss. 在极少数情况下,如果您的硬盘驱动器由于断电而无法写入数据,则可以看到此类错误。 Chkdsk should help Chkdsk应该帮忙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM