S
S
Shato Daltsaev2017-05-06 08:18:32
C++ / C#
Shato Daltsaev, 2017-05-06 08:18:32

LINQ and big table, how to solve Count problem?

Can't solve problem with collection size calculation. There is a table on the site with filters, 10 records are displayed there.

data = dc.books.OrderBy(x => x.id).Where(x =>
                                     (market_id == 0 || x.market_id != 0 && x.market_id == market_id)
                                     && (name == null || x.name != null && (x.name.StartsWith(name)))
                                     && (author == null || x.author != null && (x.author.StartsWith(author)))
                                     ).Skip(param.Start).Take(10).ToList();

Everything is fast and works well. Well, I need to calculate the size of the resulting collection.
count = db.books.Count(x =>
                    (market_id == 0 || x.market_id != 0 && x.market_id == market_id)
                    && (name == null || x.name != null && (x.name.StartsWith(name)))

Under one condition, everything works quickly, but as soon as I add two other conditions, everything hangs up. Base on Mysql ~ 500,000. And in general, what is the best way to solve this problem with large tables? How can I solve this problem, please help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Gavrilenko, 2017-05-06
@Maddox

Can AsNoTracking speed up query execution?

count = db.books.AsNoTracking().Count(x =>
                    (market_id == 0 || x.market_id != 0 && x.market_id == market_id)
                    && (name == null || x.name != null && (x.name.StartsWith(name)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question