我有一个用例,用户可以输入文本,系统会搜索匹配的发票。键入的文本可以与发票上的多个字段匹配,也可以匹配发票行。
用户只能访问基于角色的所有发票的子集。但这可能是数千张发票的清单。
搜索是通过简单的like搜索与通配符相结合来完成的。(查询用hql编写)
在显示结果时,我们希望将其限制为50,因为不会根据用户搜索的内容来选择/处理数千个条目。但是,按照我加入的方式,这个限制不能由数据库强制执行。
我们曾经有类似(伪代码)的东西
select * from tbl_invoice
join tbl_useraccess .... :userid //a few joins happen here to limit to invoices
//where the user has access to
where number like :input
or name like :input
or id in (select id from InvoiceLine where reference like :input)
limit top 50这有非常糟糕的性能,因为搜索是在每个发票行上进行的,而不是只搜索您有权访问的那些行,但总是给出正确的50行。
我已将其更改为
select * from tbl_invoice invoice
join tbl_useraccess .... :userid
join tbl_invoiceline line on invoice.id = line.invoice_id
where number like :input
or name like :input
or line.reference like :input
limit top 50性能要好得多(前面的语句只会超时),但限制不起作用,因为一张发票可能有多行。
我们可以从数据库中检索所有结果,将其映射到java对象,并在Java中执行最多50个结果,但我担心如果用户检索数千-数百万条记录,这可能会耗尽我们的内存。
因此,总而言之,我正在寻找一种更好的方法来检索固定的结果列表,同时还能够在链接的1-n实体中进行搜索
发布于 2019-01-24 19:03:55
select * from InvoiceLine line where reference like :input
join tbl_invoice invoice on invoice.id = line.invoice_id
join tbl_useraccess .... :userid
where number like :input
or name like :input
or line.reference like :input
limit top 50这将限制您的发票行数为50。
https://stackoverflow.com/questions/54344992
复制相似问题