I am experiencing a weird behavior using select query IN clause with a collection (I am using MSSQL on Azure).
I have the following code:
final Query query = em.createNativeQuery("select p.id as productId, p.external_id as externalId from products p where p.id in (:ids)");
// the ids list is of size 750
query.setParameter("ids", ids);
query.getResultList();
This code takes about 4 minutes to run, however running the query directly from the MSSQL client, it takes 1-2 seconds.
I was suspecting the in clause and I modified the code:
final String queryString = "select p.id as productId, p.external_id as externalId from products p where p.id in (%s)";
String inClause = ...; // generate a string of type 'id1', 'id2',...'id750'
final Query query = em.createNativeQuery(String.format(queryString, inClause));
query.getResultList();
The second version takes only 10 seconds to run.
Do you know any specific known problem running in clause queries? Can you suggest any solution how to get this working?
UPDATE:
I found out that the problem is actually happening with parameterized query, I tried to use a jdbc template and use my own wrapper to iterate over the results set. I iterated over the results set only printed the index, I could see that after about 590 results the results set hangs for about 3-4 minutes and then continues and finishes.
UPDATE: the id column which is the PK, is a char[36] column
I can see the query generated on Azure MSSSQL:
(#P749 nvarchar(4000),#P748 nvarchar(4000),#P747 nvarchar(4000),#P746 nvarchar(4000),#P745 nvarchar(4000),#P744 nvarchar(4000),#P743 nvarchar(4000),#P742 nvarchar(4000),#P741 nvarchar(4000),#P740 nvarchar(4000),#P739 nvarchar(4000),#P738 nvarchar(4000),#P737 nvarchar(4000),#P736 nvarchar(4000),#P735 nvarchar(4000),#P734 nvarchar(4000),#P733 nvarchar(4000),#P732 nvarchar(4000),#P731 nvarchar(4000),#P730 nvarchar(4000),#P729 nvarchar(4000),#P728 nvarchar(4000),#P727 nvarchar(4000),#P726 nvarchar(4000),#P725 nvarchar(4000),#P724 nvarchar(4000),#P723 nvarchar(4000),#P722 nvarchar(4000),#P721 nvarchar(4000),#P720 nvarchar(4000),#P719 nvarchar(4000),#P718 nvarchar(4000),#P717 nvarchar(4000),#P716 nvarchar(4000),#P715 nvarchar(4000),#P714 nvarchar(4000),#P713 nvarchar(4000),#P712 nvarchar(4000),#P711 nvarchar(4000),#P710 nvarchar(4000),#P709 nvarchar(4000),#P708 nvarchar(4000),#P707 nvarchar(4000),#P706 nvarchar(4000),#P705 nvarchar(4000),#P704 nvarchar(4000),#P703 nvarchar(4000),#P702 nvarchar(4000),#P701 nvarchar(4000),#P700 nvarchar(4000),#P699 nvarchar(4000),#P698 nvarchar(4000),#P697 nvarchar(4000),#P696 nvarchar(4000),#P695 nvarchar(4000),#P694 nvarchar(4000),#P693 nvarchar(4000),#P692 nvarchar(4000),#P691 nvarchar(4000),#P690 nvarchar(4000),#P689 nvarchar(4000),#P688 nvarchar(4000),#P687 nvarchar(4000),#P686 nvarchar(4000),#P685 nvarchar(4000),#P684 nvarchar(4000),#P683 nvarchar(4000),#P682 nvarchar(4000),#P681 nvarchar(4000),#P680 nvarchar(4000),#P679 nvarchar(4000),#P678 nvarchar(4000),#P677 nvarchar(4000),#P676 nvarchar(4000),#P675 nvarchar(4000),#P674 nvarchar(4000),#P673 nvarchar(4000),#P672 nvarchar(4000),#P671 nvarchar(4000),#P670 nvarchar(4000),#P669 nvarchar(4000),#P668 nvarchar(4000),#P667 nvarchar(4000),#P666 nvarchar(4000),#P665 nvarchar(4000),#P664 nvarchar(4000),#P663 nvarchar(4000),#P662 nvarchar(4000),#P661 nvarchar(4000),#P660 nvarchar(4000),#P659 nvarchar(4000),#P658 nvarchar(4000),#P657 nvarchar(4000),#P656 nvarchar(4000),#P655 nvarchar(4000),#P654 nvarchar(4000),#P653 nvarchar(4000),#P652 nvarchar(4000),#P651 nvarchar(4000),#P650 nvarchar(4000),#P649 nvarchar(4000),#P648 nvarchar(4000),#P647 nvarchar(4000),#P646 nvarchar(4000),#P645 nvarchar(4000),#P644 nvarchar(4000),#P643 nvarchar(4000),#P642 nvarchar(4000),#P641 nvarchar(4000),#P640 nvarchar(4000),#P639 nvarchar(4000),#P638 nvarchar(4000),#P637 nvarchar(4000),#P636 nvarchar(4000),#P635 nvarchar(4000),#P634 nvarchar(4000),#P633 nvarchar(4000),#P632 nvarchar(4000),#P631 nvarchar(4000),#P630 nvarchar(4000),#P629 nvarchar(4000),#P628 nvarchar(4000),#P627 nvarchar(4000),#P626 nvarchar(4000),#P625 nvarchar(4000),#P624 nvarchar(4000),#P623 nvarchar(4000),#P622 nvarchar(4000),#P621 nvarchar(4000),#P620 nvarchar(4000),#P619 nvarchar(4000),#P618 nvarchar(4000),#P617 nvarchar(4000),#P616 nvarchar(4000),#P615 nvarchar(4000),#P614 nvarchar(4000),#P613 nvarchar(4000),#P612 nvarchar(4000),#P611 nvarchar(4000),#P610 nvarchar(4000),#P609 nvarchar(4000),#P608 nvarchar(4000),#P607 nvarchar(4000),#P606 nvarchar(4000),#P605 nvarchar(4000),#P604 nvarchar(4000),#P603 nvarchar(4000),#P602 nvarchar(4000),#P601 nvarchar(4000),#P600 nvarchar(4000),#P599 nvarchar(4000),#P598 nvarchar(4000),#P597 nvarchar(4000),#P596 nvarchar(4000),#P595 nvarchar(4000),#P594 nvarchar(4000),#P593 nvarchar(4000),#P592 nvarchar(4000),#P591 nvarchar(4000),#P590 nvarchar(4000),#P589 nvarchar(4000),#P588 nvarchar(4000),#P587 nvarchar(4000),#P586 nvarchar(4000),#P585 nvarchar(4000),#P584 nvarchar(4000),#P583 nvarchar(4000),#P582 nvarchar(4000),#P581 nvarchar(4000),#P580 nvarchar(4000),#P579 nvarchar(4000),#P578 nvarchar(4000),#P577 nvarchar(4000),#P576 nvarchar(4000),#P575 nvarchar(4000),#P574 nvarchar(4000),#P573 nvarchar(4000),#P572 nvarchar(4000),#P571 nvarchar(4000),#P570 nvarchar(4000),#P569 nvarchar(4000),#P568 nvarchar(4000),#P567 nvarchar(4000),#P566 nvarchar(4000),#P565 nvarchar(4000),#P564 nvarchar(4000),#P563 nvarchar(4000),#P562 nvarchar(4000),#P561 nvarchar(4000),#P560 nvarchar(4000),#P559 nvarchar(4000),#P558 nvarchar(4000),#P557 nvarchar(4000),#P556 nvarchar(4000),#P555 nvarchar(4000),#P554 nvarchar(4000),#P553 nvarchar(4000),#P552 nvarchar(4000),#P551 nvarchar(4000),#P550 nvarchar(4000),#P549 nvarchar(4000),#P548 nvarchar(4000),#P547 nvarchar(4000),#P546 nvarchar(4000),#P545 nvarchar(4000),#P544 nvarchar(4000),#P543 nvarchar(4000),#P542 nvarchar(4000),#P541 nvarchar(4000),#P540 nvarchar(4000),#P539 nvarchar(4000),#P538 nvarchar(4000),#P537 nvarchar(4000),#P536 nvarchar(4000),#P535 nvarchar(4000),#P534 nvarchar(4000),#P533 nvarchar(4000),#P532 nvarchar(4000),#P531 nvarchar(4000),#P530 nvarchar(4000),#P529 nvarchar(4000),#P528 nvarchar(4000),#P527 nvarchar(4000),#P526 nvarchar(4000),#P525 nvarchar(4000),#P524 nvarchar(4000),#P523 nvarchar(4000),#P522 nvarchar(4000),#P521 nvarchar(4000),#P520 nvarchar(4000),#P519 nvarchar(4000),#P518 nvarchar(4000),#P517 nvarchar(4000),#P516 nvarchar(4000),#P515 nvarchar(4000),#P514 nvarchar(4000),#P513 nvarchar(4000),#P512 nvarchar(4000),#P511 nvarchar(4000),#P510 nvarchar(4000),#P509 nvarchar(4000),#P508 nvarchar(4000),#P507 nvarchar(4000),#P506 nvarchar(4000),#P505 nvarchar(4000),#P504 nvarchar(4000),#P503 nvarchar(4000),#P502 nvarchar(4000),#P501 nvarchar(4000),#P500 nvarchar(4000),#P499 nvarchar(4000),#P498 nvarchar(4000),#P497 nvarchar(4000),#P496 nvarchar(4000),#P495 nvarchar(4000),#P494 nvarchar(4000),#P493 nvarchar(4000),#P492 nvarchar(4000),#P491 nvarchar(4000),#P490 nvarchar(4000),#P489 nvarchar(4000),#P488 nvarchar(4000),#P487 nvarchar(4000),#P486 nvarchar(4000),#P485 nvarchar(4000),#P484 nvarchar(4000),#P483 nvarchar(4000),#P482 nvarchar(4000),#P481 nvarchar(4000),#P480 nvarchar(4000),#P479 nvarchar(4000),#P478 nvarchar(4000),#P477 nvarchar(4000),#P476 nvarchar(4000),#P475 nvarchar(4000),#P474 nvarchar(4000),#P473 nvarchar(4000),#P472 nvarchar(4000),#P471 nvarchar(4000),#P470 nvarchar(4000),#P469 nvarchar(4000),#P468 nvarchar(4000),#P467 nvarchar(4000),#P466 nvarchar(4000),#P465 nvarchar(4000),#P464 nvarchar(4000),#P463 nvarchar(4000),#P462 nvarchar(4000),#P461 nvarchar(4000),#P460 nvarchar(4000),#P459 nvarchar(4000),#P458 nvarchar(4000),#P457 nvarchar(4000),#P456 nvarchar(4000),#P455 nvarchar(4000),#P454 nvarchar(4000),#P453 nvarchar(4000),#P452 nvarchar(4000),#P451 nvarchar(4000),#P450 nvarchar(4000),#P449 nvarchar(4000),#P448 nvarchar(4000),#P447 nvarchar(4000),#P446 nvarchar(4000),#P445 nvarchar(4000),#P444 nvarchar(4000),#P443 nvarchar(4000),#P442 nvarchar(4000),#P441 nvarchar(4000),#P440 nvarchar(4000),#P439 nvarchar(4000),#P438 nvarchar(4000),#P437 nvarchar(4000),#P436 nvarchar(4000),#P435 nvarchar(4000),#P434 nvarchar(4000),#P433 nvarchar(4000),#P432 nvarchar(4000),#P431 nvarchar(4000),#P430 nvarchar(4000),#P429 nvarchar(4000),#P428 nvarchar(4000),#P427 nvarchar(4000),#P426 nvarchar(4000),#P425 nvarchar(4000),#P424 nvarchar(4000),#P423 nvarchar(4000),#P422 nvarchar(4000),#P421 nvarchar(4000),#P420 nvarchar(4000),#P419 nvarchar(4000),#P418 nvarchar(4000),#P417 nvarchar(4000),#P416 nvarchar(4000),#P415 nvarchar(4000),#P414 nvarchar(4000),#P413 nvarchar(4000),#P412 nvarchar(4000),#P411 nvarchar(4000),#P410 nvarchar(4000),#P409 nvarchar(4000),#P408 nvarchar(4000),#P407 nvarchar(4000),#P406 nvarchar(4000),#P405 nvarchar(4000),#P404 nvarchar(4000),#P403 nvarchar(4000),#P402 nvarchar(4000),#P401 nvarchar(4000),#P400 nvarchar(4000),#P399 nvarchar(4000),#P398 nvarchar(4000),#P397 nvarchar(4000),#P396 nvarchar(4000),#P395 nvarchar(4000),#P394 nvarchar(4000),#P393 nvarchar(4000),#P392 nvarchar(4000),#P391 nvarchar(4000),#P390 nvarchar(4000),#P389 nvarchar(4000),#P388 nvarchar(4000),#P387 nvarchar(4000),#P386 nvarchar(4000),#P385 nvarchar(4000),#P384 nvarchar(4000),#P383 nvarchar(4000),#P382 nvarchar(4000),#P381 nvarchar(4000),#P380 nvarchar(4000),#P379 nvarchar(4000),#P378 nvarchar(4000),#P377 nvarchar(4000),#P376 nvarchar(4000),#P375 nvarchar(4000),#P374 nvarchar(4000),#P373 nvarchar(4000),#P372 nvarchar(4000),#P371 nvarchar(4000),#P370 nvarchar(4000),#P369 nvarchar(4000),#P368 nvarchar(4000),#P367 nvarchar(4000),#P366 nvarchar(4000),#P365 nvarchar(4000),#P364 nvarchar(4000),#P363 nvarchar(4000),#P362 nvarchar(4000),#P361 nvarchar(4000),#P360 nvarchar(4000),#P359 nvarchar(4000),#P358 nvarchar(4000),#P357 nvarchar(4000),#P356 nvarchar(4000),#P355 nvarchar(4000),#P354 nvarchar(4000),#P353 nvarchar(4000),#P352 nvarchar(4000),#P351 nvarchar(4000),#P350 nvarchar(4000),#P349 nvarchar(4000),#P348 nvarchar(4000),#P347 nvarchar(4000),#P346 nvarchar(4000),#P345 nvarchar(4000),#P344 nvarchar(4000),#P343 nvarchar(4000),#P342 nvarchar(4000),#P341 nvarchar(4000),#P340 nvarchar(4000),#P339 nvarchar(4000),#P338 nvarchar(4000),#P337 nvarchar(4000),#P336 nvarchar(4000),#P335 nvarchar(4000),#P334 nvarchar(4000),#P333 nvarchar(4000),#P332 nvarchar(4000),#P331 nvarchar(4000),#P330 nvarchar(4000),#P329 nvarchar(4000),#P328 nvarchar(4000),#P327 nvarchar(4000),#P326 nvarchar(4000),#P325 nvarchar(4000),#P324 nvarchar(4000),#P323 nvarchar(4000),#P322 nvarchar(4000),#P321 nvarchar(4000),#P320 nvarchar(4000),#P319 nvarchar(4000),#P318 nvarchar(4000),#P317 nvarchar(4000),#P316 nvarchar(4000),#P315 nvarchar(4000),#P314 nvarchar(4000),#P313 nvarchar(4000),#P312 nvarchar(4000),#P311 nvarchar(4000),#P310 nvarchar(4000),#P309 nvarchar(4000),#P308 nvarchar(4000),#P307 nvarchar(4000),#P306 nvarchar(4000),#P305 nvarchar(4000),#P304 nvarchar(4000),#P303 nvarchar(4000),#P302 nvarchar(4000),#P301 nvarchar(4000),#P300 nvarchar(4000),#P299 nvarchar(4000),#P298 nvarchar(4000),#P297 nvarchar(4000),#P296 nvarchar(4000),#P295 nvarchar(4000),#P294 nvarchar(4000),#P293 nvarchar(4000),#P292 nvarchar(4000),#P291 nvarchar(4000),#P290 nvarchar(4000),#P289 nvarchar(4000),#P288 nvarchar(4000),#P287 nvarchar(4000),#P286 nvarchar(4000),#P285 nvarchar(4000),#P284 nvarchar(4000),#P283 nvarchar(4000),#P282 nvarchar(4000),#P281 nvarchar(4000),#P280 nvarchar(4000),#P279 nvarchar(4000),#P278 nvarchar(4000),#P277 nvarchar(4000),#P276 nvarchar(4000),#P275 nvarchar(4000),#P274 nvarchar(4000),#P273 nvarchar(4000),#P272 nvarchar(4000),#P271 nvarchar(4000),#P270 nvarchar(4000),#P269 nvarchar(4000),#P268 nvarchar(4000),#P267 nvarchar(4000),#P266 nvarchar(4000),#P265 nvarchar(4000),#P264 nvarchar(4000),#P263 nvarchar(4000),#P262 nvarchar(4000),#P261 nvarchar(4000),#P260 nvarchar(4000),#P259 nvarchar(4000),#P258 nvarchar(4000),#P257 nvarchar(4000),#P256 nvarchar(4000),#P255 nvarchar(4000),#P254 nvarchar(4000),#P253 nvarchar(4000),#P252 nvarchar(4000),#P251 nvarchar(4000),#P250 nvarchar(4000),#P249 nvarchar(4000),#P248 nvarchar(4000),#P247 nvarchar(4000),#P246 nvarchar(4000),#P245 nvarchar(4000),#P244 nvarchar(4000),#P243 nvarchar(4000),#P242 nvarchar(4000),#P241 nvarchar(4000),#P240 nvarchar(4000),#P239 nvarchar(4000),#P238 nvarchar(4000),#P237 nvarchar(4000),#P236 nvarchar(4000),#P235 nvarchar(4000),#P234 nvarchar(4000),#P233 nvarchar(4000),#P232 nvarchar(4000),#P231 nvarchar(4000),#P230 nvarchar(4000),#P229 nvarchar(4000),#P228 nvarchar(4000),#P227 nvarchar(4000),#P226 nvarchar(4000),#P225 nvarchar(4000),#P224 nvarchar(4000),#P223 nvarchar(4000),#P222 nvarchar(4000),#P221 nvarchar(4000),#P220 nvarchar(4000),#P219 nvarchar(4000),#P218 nvarchar(4000),#P217 nvarchar(4000),#P216 nvarchar(4000),#P215 nvarchar(4000),#P214 nvarchar(4000),#P213 nvarchar(4000),#P212 nvarchar(4000),#P211 nvarchar(4000),#P210 nvarchar(4000),#P209 nvarchar(4000),#P208 nvarchar(4000),#P207 nvarchar(4000),#P206 nvarchar(4000),#P205 nvarchar(4000),#P204 nvarchar(4000),#P203 nvarchar(4000),#P202 nvarchar(4000),#P201 nvarchar(4000),#P200 nvarchar(4000),#P199 nvarchar(4000),#P198 nvarchar(4000),#P197 nvarchar(4000),#P196 nvarchar(4000),#P195 nvarchar(4000),#P194 nvarchar(4000),#P193 nvarchar(4000),#P192 nvarchar(4000),#P191 nvarchar(4000),#P190 nvarchar(4000),#P189 nvarchar(4000),#P188 nvarchar(4000),#P187 nvarchar(4000),#P186 nvarchar(4000),#P185 nvarchar(4000),#P184 nvarchar(4000),#P183 nvarchar(4000),#P182 nvarchar(4000),#P181 nvarchar(4000),#P180 nvarchar(4000),#P179 nvarchar(4000),#P178 nvarchar(4000),#P177 nvarchar(4000),#P176 nvarchar(4000),#P175 nvarchar(4000),#P174 nvarchar(4000),#P173 nvarchar(4000),#P172 nvarchar(4000),#P171 nvarchar(4000),#P170 nvarchar(4000),#P169 nvarchar(4000),#P168 nvarchar(4000),#P167 nvarchar(4000),#P166 nvarchar(4000),#P165 nvarchar(4000),#P164 nvarchar(4000),#P163 nvarchar(4000),#P162 nvarchar(4000),#P161 nvarchar(4000),#P160 nvarchar(4000),#P159 nvarchar(4000),#P158 nvarchar(4000),#P157 nvarchar(4000),#P156 nvarchar(4000),#P155 nvarchar(4000),#P154 nvarchar(4000),#P153 nvarchar(4000),#P152 nvarchar(4000),#P151 nvarchar(4000),#P150 nvarchar(4000),#P149 nvarchar(4000),#P148 nvarchar(4000),#P147 nvarchar(4000),#P146 nvarchar(4000),#P145 nvarchar(4000),#P144 nvarchar(4000),#P143 nvarchar(4000),#P142 nvarchar(4000),#P141 nvarchar(4000),#P140 nvarchar(4000),#P139 nvarchar(4000),#P138 nvarchar(4000),#P137 nvarchar(4000),#P136 nvarchar(4000),#P135 nvarchar(4000),#P134 nvarchar(4000),#P133 nvarchar(4000),#P132 nvarchar(4000),#P131 nvarchar(4000),#P130 nvarchar(4000),#P129 nvarchar(4000),#P128 nvarchar(4000),#P127 nvarchar(4000),#P126 nvarchar(4000),#P125 nvarchar(4000),#P124 nvarchar(4000),#P123 nvarchar(4000),#P122 nvarchar(4000),#P121 nvarchar(4000),#P120 nvarchar(4000),#P119 nvarchar(4000),#P118 nvarchar(4000),#P117 nvarchar(4000),#P116 nvarchar(4000),#P115 nvarchar(4000),#P114 nvarchar(4000),#P113 nvarchar(4000),#P112 nvarchar(4000),#P111 nvarchar(4000),#P110 nvarchar(4000),#P109 nvarchar(4000),#P108 nvarchar(4000),#P107 nvarchar(4000),#P106 nvarchar(4000),#P105 nvarchar(4000),#P104 nvarchar(4000),#P103 nvarchar(4000),#P102 nvarchar(4000),#P101 nvarchar(4000),#P100 nvarchar(4000),#P99 nvarchar(4000),#P98 nvarchar(4000),#P97 nvarchar(4000),#P96 nvarchar(4000),#P95 nvarchar(4000),#P94 nvarchar(4000),#P93 nvarchar(4000),#P92 nvarchar(4000),#P91 nvarchar(4000),#P90 nvarchar(4000),#P89 nvarchar(4000),#P88 nvarchar(4000),#P87 nvarchar(4000),#P86 nvarchar(4000),#P85 nvarchar(4000),#P84 nvarchar(4000),#P83 nvarchar(4000),#P82 nvarchar(4000),#P81 nvarchar(4000),#P80 nvarchar(4000),#P79 nvarchar(4000),#P78 nvarchar(4000),#P77 nvarchar(4000),#P76 nvarchar(4000),#P75 nvarchar(4000),#P74 nvarchar(4000),#P73 nvarchar(4000),#P72 nvarchar(4000),#P71 nvarchar(4000),#P70 nvarchar(4000),#P69 nvarchar(4000),#P68 nvarchar(4000),#P67 nvarchar(4000),#P66 nvarchar(4000),#P65 nvarchar(4000),#P64 nvarchar(4000),#P63 nvarchar(4000),#P62 nvarchar(4000),#P61 nvarchar(4000),#P60 nvarchar(4000),#P59 nvarchar(4000),#P58 nvarchar(4000),#P57 nvarchar(4000),#P56 nvarchar(4000),#P55 nvarchar(4000),#P54 nvarchar(4000),#P53 nvarchar(4000),#P52 nvarchar(4000),#P51 nvarchar(4000),#P50 nvarchar(4000),#P49 nvarchar(4000),#P48 nvarchar(4000),#P47 nvarchar(4000),#P46 nvarchar(4000),#P45 nvarchar(4000),#P44 nvarchar(4000),#P43 nvarchar(4000),#P42 nvarchar(4000),#P41 nvarchar(4000),#P40 nvarchar(4000),#P39 nvarchar(4000),#P38 nvarchar(4000),#P37 nvarchar(4000),#P36 nvarchar(4000),#P35 nvarchar(4000),#P34 nvarchar(4000),#P33 nvarchar(4000),#P32 nvarchar(4000),#P31 nvarchar(4000),#P30 nvarchar(4000),#P29 nvarchar(4000),#P28 nvarchar(4000),#P27 nvarchar(4000),#P26 nvarchar(4000),#P25 nvarchar(4000),#P24 nvarchar(4000),#P23 nvarchar(4000),#P22 nvarchar(4000),#P21 nvarchar(4000),#P20 nvarchar(4000),#P19 nvarchar(4000),#P18 nvarchar(4000),#P17 nvarchar(4000),#P16 nvarchar(4000),#P15 nvarchar(4000),#P14 nvarchar(4000),#P13 nvarchar(4000),#P12 nvarchar(4000),#P11 nvarchar(4000),#P10 nvarchar(4000),#P9 nvarchar(4000),#P8 nvarchar(4000),#P7 nvarchar(4000),#P6 nvarchar(4000),#P5 nvarchar(4000),#P4 nvarchar(4000),#P3 nvarchar(4000),#P2 nvarchar(4000),#P1 nvarchar(4000),#P0 nvarchar(4000))select p.id as productId, p.external_id as externalId from products p where p.id in (#P0, #P1, #P2, #P3, #P4, #P5, #P6, #P7, #P8, #P9, #P10, #P11, #P12, #P13, #P14, #P15, #P16, #P17, #P18, #P19, #P20, #P21, #P22, #P23, #P24, #P25, #P26, #P27, #P28, #P29, #P30, #P31, #P32, #P33, #P34, #P35, #P36, #P37, #P38, #P39, #P40, #P41, #P42, #P43, #P44, #P45, #P46, #P47, #P48, #P49, #P50, #P51, #P52, #P53, #P54, #P55, #P56, #P57, #P58, #P59, #P60, #P61, #P62, #P63, #P64, #P65, #P66, #P67, #P68, #P69, #P70, #P71, #P72, #P73, #P74, #P75, #P76, #P77, #P78, #P79, #P80, #P81, #P82, #P83, #P84, #P85, #P86, #P87, #P88, #P89, #P90, #P91, #P92, #P93, #P94, #P95, #P96, #P97, #P98, #P99, #P100, #P101, #P102, #P103, #P104, #P105, #P106, #P107, #P108, #P109, #P110, #P111, #P112, #P113, #P114, #P115, #P116, #P117, #P118, #P119, #P120, #P121, #P122, #P123, #P124, #P125, #P126, #P127, #P128, #P129, #P130, #P131, #P132, #P133, #P134, #P135, #P136, #P137, #P138, #P139, #P140, #P141, #P142, #P143, #P144, #P145, #P146, #P147, #P148, #P149, #P150, #P151, #P152, #P153, #P154, #P155, #P156, #P157, #P158, #P159, #P160, #P161, #P162, #P163, #P164, #P165, #P166, #P167, #P168, #P169, #P170, #P171, #P172, #P173, #P174, #P175, #P176, #P177, #P178, #P179, #P180, #P181, #P182, #P183, #P184, #P185, #P186, #P187, #P188, #P189, #P190, #P191, #P192, #P193, #P194, #P195, #P196, #P197, #P198, #P199, #P200, #P201, #P202, #P203, #P204, #P205, #P206, #P207, #P208, #P209, #P210, #P211, #P212, #P213, #P214, #P215, #P216, #P217, #P218, #P219, #P220, #P221, #P222, #P223, #P224, #P225, #P226, #P227, #P228, #P229, #P230, #P231, #P232, #P233, #P234, #P235, #P236, #P237, #P238, #P239, #P240, #P241, #P242, #P243, #P244, #P245, #P246, #P247, #P248, #P249, #P250, #P251, #P252, #P253, #P254, #P255, #P256, #P257, #P258, #P259, #P260, #P261, #P262, #P263, #P264, #P265, #P266, #P267, #P268, #P269, #P270, #P271, #P272, #P273, #P274, #P275, #P276, #P277, #P278, #P279, #P280, #P281, #P282, #P283, #P284, #P285, #P286, #P287, #P288, #P289, #P290, #P291, #P292, #P293, #P294, #P295, #P296, #P297, #P298, #P299, #P300, #P301, #P302, #P303, #P304, #P305, #P306, #P307, #P308, #P309, #P310, #P311, #P312, #P313, #P314, #P315, #P316, #P317, #P318, #P319, #P320, #P321, #P322, #P323, #P324, #P325, #P326, #P327, #P328, #P329, #P330, #P331, #P332, #P333, #P334, #P335, #P336, #P337, #P338, #P339, #P340, #P341, #P342, #P343, #P344, #P345, #P346, #P347, #P348, #P349, #P350, #P351, #P352, #P353, #P354, #P355, #P356, #P357, #P358, #P359, #P360, #P361, #P362, #P363, #P364, #P365, #P366, #P367, #P368, #P369, #P370, #P371, #P372, #P373, #P374, #P375, #P376, #P377, #P378, #P379, #P380, #P381, #P382, #P383, #P384, #P385, #P386, #P387, #P388, #P389, #P390, #P391, #P392, #P393, #P394, #P395, #P396, #P397, #P398, #P399, #P400, #P401, #P402, #P403, #P404, #P405, #P406, #P407, #P408, #P409, #P410, #P411, #P412, #P413, #P414, #P415, #P416, #P417, #P418, #P419, #P420, #P421, #P422, #P423, #P424, #P425, #P426, #P427, #P428, #P429, #P430, #P431, #P432, #P433, #P434, #P435, #P436, #P437, #P438, #P439, #P440, #P441, #P442, #P443, #P444, #P445, #P446, #P447, #P448, #P449, #P450, #P451, #P452, #P453, #P454, #P455, #P456, #P457, #P458, #P459, #P460, #P461, #P462, #P463, #P464, #P465, #P466, #P467, #P468, #P469, #P470, #P471, #P472, #P473, #P474, #P475, #P476, #P477, #P478, #P479, #P480, #P481, #P482, #P483, #P484, #P485, #P486, #P487, #P488, #P489, #P490, #P491, #P492, #P493, #P494, #P495, #P496, #P497, #P498, #P499, #P500, #P501, #P502, #P503, #P504, #P505, #P506, #P507, #P508, #P509, #P510, #P511, #P512, #P513, #P514, #P515, #P516, #P517, #P518, #P519, #P520, #P521, #P522, #P523, #P524, #P525, #P526, #P527, #P528, #P529, #P530, #P531, #P532, #P533, #P534, #P535, #P536, #P537, #P538, #P539, #P540, #P541, #P542, #P543, #P544, #P545, #P546, #P547, #P548, #P549, #P550, #P551, #P552, #P553, #P554, #P555, #P556, #P557, #P558, #P559, #P560, #P561, #P562, #P563, #P564, #P565, #P566, #P567, #P568, #P569, #P570, #P571, #P572, #P573, #P574, #P575, #P576, #P577, #P578, #P579, #P580, #P581, #P582, #P583, #P584, #P585, #P586, #P587, #P588, #P589, #P590, #P591, #P592, #P593, #P594, #P595, #P596, #P597, #P598, #P599, #P600, #P601, #P602, #P603, #P604, #P605, #P606, #P607, #P608, #P609, #P610, #P611, #P612, #P613, #P614, #P615, #P616, #P617, #P618, #P619, #P620, #P621, #P622, #P623, #P624, #P625, #P626, #P627, #P628, #P629, #P630, #P631, #P632, #P633, #P634, #P635, #P636, #P637, #P638, #P639, #P640, #P641, #P642, #P643, #P644, #P645, #P646, #P647, #P648, #P649, #P650, #P651, #P652, #P653, #P654, #P655, #P656, #P657, #P658, #P659, #P660, #P661, #P662, #P663, #P664, #P665, #P666, #P667, #P668, #P669, #P670, #P671, #P672, #P673, #P674, #P675, #P676, #P677, #P678, #P679, #P680, #P681, #P682, #P683, #P684, #P685, #P686, #P687, #P688, #P689, #P690, #P691, #P692, #P693, #P694, #P695, #P696, #P697, #P698, #P699, #P700, #P701, #P702, #P703, #P704, #P705, #P706, #P707, #P708, #P709, #P710, #P711, #P712, #P713, #P714, #P715, #P716, #P717, #P718, #P719, #P720, #P721, #P722, #P723, #P724, #P725, #P726, #P727, #P728, #P729, #P730, #P731, #P732, #P733, #P734, #P735, #P736, #P737, #P738, #P739, #P740, #P741, #P742, #P743, #P744, #P745, #P746, #P747, #P748, #P749)
I was trying to contact Azure's support and they recommended to add a FORCESEEK hint to the query - Do you know how to add this query hint to the springdata repository?
Thank you.
The problem in this case is that the parameters are handles as Unicode (nvarchar) and my indexes where Ascii [char(36)]. Due to that reason it seems the searches are not executed using the indexes which make is very small. It seems like a normal behavior since Java strings are Unicode and there is no real way for the DB to down convert Unicode to Ascii.
After modifying my id columns (PK and FK) from char(36) to nchar(36) the parameterized queries run as expected and running a search with IN clause of 2,000 ids takes about 400msec instead of almost 10 minutes.