Postgresql performance

I am new to Rust and I am trying to read 2,000,000 database postgresql with Rust.
The same code takes 8 seconds in Java but with Rust it takes about 50 seconds.
I am sure doing something wrong. (Postgres 0.18.1 - Rust 1.49.0-nightly)

Here is the code:

    let url = "postgresql://user:pass@localhost:5432/mydb";
    let client = Client::connect(url, NoTls).unwrap();


pub fn get_entries(mut client: Client)->(i32, Vec<Res1>)
    let mut count= 0;
    let mut res = Vec::new();
    for row in client.query("SELECT * FROM data", &[]).unwrap()
        //println!("{:?}", row);
        let r = Res1 {
            index: row.get(0),
            mdate: row.get(1),
            mtime: row.get(2),
            bid: row.get(3),
            ask: row.get(4),
            last: row.get(5),
            volume: row.get(6),

        count += 1;

    (count, res)

Any idea what am I doing wrong?


Are you sure, in your java version, you are actually building the same result set array in memory?

(You could try pre-allocating a larger Vec, but I really doubt that is the problem.)

Edit: Also to improve your chances of being helped, could you re-format your post using ``` delimiters, and include your main function? For example:

use rust.formatted.code.blocks;

Please read Forum Code Formatting and Syntax Highlighting and edit your initial post, as @dekellum requested, by using the pencil-shaped edit button under that post.

Many readers of this forum will ignore code snippets, compiler error reports, etc that do not follow the posting guidelines for new contributors that are pinned to the top of this forum. Even those who do respond may feel that the lack of following the forum posting guidelines is disrespectful of their time. Thanks. :clap:

When comparing timing of Rust code vs. other languages, always run rustc in release mode, not in the default debug mode. It is not uncommon for debug-mode code to run 10x to 100x slower than release mode. There are dozens of threads in this forum about timing Rust code and optimizing performance; all of them start with the warning to not test debug-mode code.

I won't look at the code, but the usual first question coming up is, did you compile with --release option? I don't know what the library does under the hood, so it's unclear to me how debug/release mode affects performance for this particular case.

Yes here is the Java code. It actually builds an arraylist in memory.

        List<NQ_entry> res = new ArrayList<>();
        int t = 0;

            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * from data");

            while (
                NQ_entry r1 = new NQ_entry();
                r1.index = rs.getInt(1);
                r1.mdate = rs.getString(2);
                r1.mtime = rs.getString(3);
       = rs.getDouble(4);
                r1.ask = rs.getDouble(5);
                r1.last = rs.getDouble(6);
                r1.volume = rs.getInt(7);

        } catch (SQLException ex)
            Logger.getLogger(DBHandling.class.getName()).log(Level.SEVERE, null, ex);

        System.out.printf("Found %d entries\n", t);
        num_entries = t;

        return res;

Thanks, with the --release I got 9sec.