How to execute multiple queries in parallel instead of sequentially?

前端 未结 2 790
南笙
南笙 2021-01-07 09:51

I am querying all my 10 tables to get the user id from them and loading all the user id\'s into HashSet so that I can have unique user id.

As of now it is sequential

2条回答
  •  长情又很酷
    2021-01-07 10:16

    You may be able to make it multithreaded but with the overhead of thread creation and multiple connections, you probably won't have significant benefit. Instead, use a UNION statement in mysql and get them all at once. Let the database engine figure out how to get them all efficiently:

    String sql = "select user_id from testkeyspace.test_table_1 UNION select  user_id from testkeyspace.test_table_2 UNION select user_id from testkeyspace.test_table_3 ...."
    

    Of course, you'll have to programatically create the sql query string. Don't actually put "...." in your query.

提交回复
热议问题