Adding column to select statement in SQL causes timeout

I have a scheduled job that processes documents stored in tables in a SQL database.
It processes documents “10 at a time” and moves the binary document data to another database and has been running for about a week continously. (There are A LOT of documents to process)

Today the job got a timeout and I’ve been trying to figure out the cause. At first I suspected that it could be some big file but this does not seem to be the issue.

Instead, I’ve noticed that there seems to be some issue with a specific column and this seems to be the column “filename”. This column is just a type “nvarchar” though so it’s so weird.

My findings are that if I simply comment the column “filename” so it is not a part of the select then the query works. If I add it (and I need it!) then I get a timeout. Also, even if I don’t add it to the select but instead add it to an “order by filename” or “group by filename” then I also get a timeout.

My query, that returns about 3600 records, is the one down below. In the example that I’ve posted below I have removed my “top 10”. The result is the same though, whenever adding “filename” I get timeout.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>select
att.AttachmentId,
att.FileSize,
att.MimeType,
att.FileName
from Attachment att
join ActivityMimeAttachment ama on att.AttachmentId = ama.AttachmentId
join ActivityPointerBase apb on apb.ActivityId = ama.ObjectId
where apb.CreatedOn < (GETDATE() - 1095)
and att.Body is not null
order by att.FileSize desc
--option (recompile)
</code>
<code>select att.AttachmentId, att.FileSize, att.MimeType, att.FileName from Attachment att join ActivityMimeAttachment ama on att.AttachmentId = ama.AttachmentId join ActivityPointerBase apb on apb.ActivityId = ama.ObjectId where apb.CreatedOn < (GETDATE() - 1095) and att.Body is not null order by att.FileSize desc --option (recompile) </code>
select 
 att.AttachmentId,
 att.FileSize,
 att.MimeType,
 att.FileName
from Attachment att
join ActivityMimeAttachment ama on att.AttachmentId = ama.AttachmentId
join ActivityPointerBase apb on apb.ActivityId = ama.ObjectId
where apb.CreatedOn < (GETDATE() - 1095)
and att.Body is not null
order by att.FileSize desc
--option (recompile)

It just seems so weird that this started happening. I would really like to know the name of the potential file that causes the timeout but the irony is that I cannot see it because adding the filename causes the issue. But I my program has processed 1.8 million files so far without issue and now all of a sudden this happens..

Any ideas or suggestions are welcome.
Also worth mentioning is that I’m using “Dynamics 365 Linq” to do the select which translates into this SQL query. So making a stored procedure is not an option. I could however make some index on the database perhaps but like I mentioned, I’ve processed a huge amount of files so far without issues..

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật