I notice that queries using values like https://query.wikidata.org/#SELECT%20%3Fitem%20%3Fitem2%20%3Finv%20WHERE%20%7B%0A%20%20VALUES%20%3Fcollection%20%7Bwd%3AQ28045665%20wd%3AQ28045660%20wd%3AQ28045674%20wd%3AQ2066737%20wd%3AQ18600731%20%7D%20.%20%23%20%20The%20different%20collections%0A%20%20%3Fitem%20p%3AP217%20%3Finv1statement%20.%0A%20%20%3Finv1statement%20ps%3AP217%20%3Finv%20.%0A%20%20%3Finv1statement%20pq%3AP195%20%3Fcollection%20.%20%0A%20%20%3Fitem2%20p%3AP217%20%3Finv2statement%20.%0A%20%20%3Finv2statement%20ps%3AP217%20%3Finv%20.%0A%20%20%3Finv2statement%20pq%3AP195%20%3Fcollection%20.%20%0A%20%20FILTER%28%3Fitem%21%3D%3Fitem2%29%0A%0A%20%20%7D%20LIMIT%20500%0A often time out.
if you unpack it in a bunch of unions it completes in less than a second: https://query.wikidata.org/#SELECT%20%3Fitem%20%3Fitem2%20%3Finv%20WHERE%20%7B%0A%7B%20%3Finv1statement%20pq%3AP195%20wd%3AQ28045665%20.%20%7D%20union%0A%7B%20%3Finv1statement%20pq%3AP195%20wd%3AQ28045660%20.%20%7D%20union%0A%7B%20%3Finv1statement%20pq%3AP195%20wd%3AQ28045674%20.%20%7D%20union%0A%7B%20%3Finv1statement%20pq%3AP195%20wd%3AQ2066737%20.%20%7D%20union%0A%7B%20%3Finv1statement%20pq%3AP195%20wd%3AQ18600731%20.%20%7D%0A%0A%20%20%3Fitem%20p%3AP217%20%3Finv1statement%20.%0A%20%20%3Finv1statement%20ps%3AP217%20%3Finv%20.%0A%0A%7B%20%3Finv2statement%20pq%3AP195%20wd%3AQ28045665%20.%20%7D%20union%0A%7B%20%3Finv2tatement%20pq%3AP195%20wd%3AQ28045660%20.%20%7D%20union%0A%7B%20%3Finv2statement%20pq%3AP195%20wd%3AQ28045674%20.%20%7D%20union%0A%7B%20%3Finv2statement%20pq%3AP195%20wd%3AQ2066737%20.%20%7D%20union%0A%7B%20%3Finv2statement%20pq%3AP195%20wd%3AQ18600731%20.%20%7D%0A%0A%20%20%3Fitem2%20p%3AP217%20%3Finv2statement%20.%0A%20%20%3Finv2statement%20ps%3AP217%20%3Finv%20.%0A%20%20FILTER%28%3Fitem%21%3D%3Fitem2%29%0A%0A%20%20%7D%20LIMIT%20500%0A%20
This is something the optimizer should handle. Optimizer output at https://query.wikidata.org/bigdata/namespace/wdq/sparql?explain&query=SELECT%20%3Fitem%20%3Fitem2%20%3Finv%20WHERE%20{%0A%20%20VALUES%20%3Fcollection%20{wd%3AQ28045665%20wd%3AQ28045660%20wd%3AQ28045674%20wd%3AQ2066737%20wd%3AQ18600731%20}%20.%20%23%20%20The%20different%20collections%0A%20%20%3Fitem%20p%3AP217%20%3Finv1statement%20.%0A%20%20%3Finv1statement%20ps%3AP217%20%3Finv%20.%0A%20%20%3Finv1statement%20pq%3AP195%20%3Fcollection%20.%20%0A%20%20%3Fitem2%20p%3AP217%20%3Finv2statement%20.%0A%20%20%3Finv2statement%20ps%3AP217%20%3Finv%20.%0A%20%20%3Finv2statement%20pq%3AP195%20%3Fcollection%20.%20%0A%20%20FILTER(%3Fitem!%3D%3Fitem2)%0A%0A%20%20}%20LIMIT%20500%0A