如何在OrientDB中选择随机文档

时间:2016-02-26 06:51:25

标签: orientdb

我写在线游戏。对于游戏逻辑,我需要从数据库中选择随机用户(等)。如何用java api实现这个目标?最有效的方法是什么?

我可以使用类似(伪代码)的内容:select from User skip(randomNum(0,usersCount)) limit 1但是如何写入文档 - skip的性能不佳。

2 个答案:

答案 0 :(得分:0)

我尝试过这段代码

1

让我知道它是否适合您?

答案 1 :(得分:0)

我写了两个java类,两个都是从特定集群中获取X随机用户。

第一个对我来说速度更快。 (约0.8s vs 1.2s)

testRandom.java

    public class testRandom {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

                String nomeDb = "RandomUser";

                try {
                    OServerAdmin serverAdmin = new OServerAdmin("remote:localhost/"+nomeDb).connect("root", "root");
                    if(serverAdmin.existsDatabase()){  // il db esiste
                        //connessione a db
                        OrientGraph g = new OrientGraph("remote:localhost/"+nomeDb);

                        //------------------------------------------------
                        long Tbegin,Tend;
                        float millis;
                        Tbegin = System.currentTimeMillis();

                        int numberRandom= 5;
                        int random;
                        String cluster = "user";
                        Iterable<Vertex> vertices = g.command(new OCommandSQL("select from cluster:"+cluster)).execute();
                        List<Vertex> v_array = new ArrayList<Vertex>();
                        List<Vertex> res = new ArrayList<Vertex>();

                        for(Vertex v : vertices){
                            v_array.add(v);
                        }
                        int arraysize = v_array.size();
                        for(int i=0;i<numberRandom;i++){
                            random=ThreadLocalRandom.current().nextInt(0, arraysize);
                            res.add(v_array.get(random));
                        }

                        for(Vertex v : res){
                            System.out.println(v.getId());
                        }


                        Tend = System.currentTimeMillis();
                        millis = (Tend-Tbegin);
                        System.out.println("--Execution time: "+millis/1000+ "s\n");

                        //------------------------------------------------

                        //chiude db
                        g.shutdown();
                    }
                    else{
                        System.out.println("Il database '"+ nomeDb + "' non esiste");
                    }
                    serverAdmin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

    }

}

testRandomSkip.java

public class testRandom_skip {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

                String nomeDb = "RandomUser";

                try {
                    OServerAdmin serverAdmin = new OServerAdmin("remote:localhost/"+nomeDb).connect("root", "root");
                    if(serverAdmin.existsDatabase()){  // il db esiste
                        //connessione a db
                        OrientGraph g = new OrientGraph("remote:localhost/"+nomeDb);

                        //------------------------------------------------
                        long Tbegin,Tend;
                        float millis;
                        Tbegin = System.currentTimeMillis();

                        int numberRandom= 5;
                        int random;
                        String cluster = "user";

                        List<Vertex> res = new ArrayList<Vertex>();
                        Iterable<Vertex> q_count_V = g.command(new OCommandSQL("select count(*) from cluster:"+cluster)).execute();
                        Long count_V = 0l;
                        for(Vertex v : q_count_V){
                            count_V=v.getProperty("count");
                            break;
                        }

                        for(int i=0;i<numberRandom;i++){
                            random=(int)ThreadLocalRandom.current().nextLong(0, count_V);
                            Iterable<Vertex> vertex = g.command(new OCommandSQL("select from cluster:"+cluster+" skip "+random+" limit 1")).execute();
                            for(Vertex v : vertex){
                                res.add(v);
                                break;
                            }
                        }

                        for(Vertex v : res){
                            System.out.println(v.getId());
                        }


                        Tend = System.currentTimeMillis();
                        millis = (Tend-Tbegin);
                        System.out.println("--Execution time: "+millis/1000+ "s\n");

                        //------------------------------------------------

                        //chiude db
                        g.shutdown();
                    }
                    else{
                        System.out.println("Il database '"+ nomeDb + "' non esiste");
                    }
                    serverAdmin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

    }

}

希望它有所帮助。 伊万