0

e.g. `

 where like '%str%' vs stream().filter(i -> i.contains(str))
    group by column a vs stream().groupingby(column_a)
    group by column_a, column_b vs stream().groupingby(column_a, groupingby(column_b))
    count vs stream().count
    sum vs stream().sum
    order by vs stream().sort

... `

I'd like to know which is faster and performance is better when it is performed in same spec server or different, thinks.

dhS
  • 3,739
  • 5
  • 26
  • 55
fanfever
  • 59
  • 7

1 Answers1

2

Those are different usecases. Database were designed to work with persisted data, streams traverse given data structures. So to use streams, you would need to read the data somehow first, that is where you use the database with indexes that helps especially with %like%.

So should the question be whether it is faster to do sum, group by, count, order by in database as part of the query or fetch the data from database and do those operations with java, then you hardly find a case where you doing it by streams would be preferable.

The database is made to be fast, streams are made to be sexy to work with. You can find stream performance related questions (here, here, and you can find others) that shows that the streams are really slow. The streams can work in parallel to become faster, but the the database do that as well.

Community
  • 1
  • 1
jan.supol
  • 2,636
  • 1
  • 26
  • 31
  • thx, so i can think no matter how complex the aggregate operate may be, database operate is better to stream? – fanfever Jun 30 '16 at 01:42
  • I remember situation back in 90s, the bank used a mainframe with just a single processor with so many clients connected to it, that when for certain sql queries that took almost a second, we were asked to filter the results on client side, even though it took significantly longer there, but the other clients were not blocked for that time. But yes, in general, the database is much faster than streams. – jan.supol Jun 30 '16 at 06:48