Performance during searching data in DB vs Memory [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am currently working with a PostgreSQL database, Spring and Hibernate. I have one table where attribute correlation_id is unique. Each time before I add a new element first I have to check does any item with a new correlation_id already exist in db or not.
For this case I have implemented recursive function that will generate a new correlation_id and check does it exist or not in db. It means this function will make a call on db each time so sometimes it can be just one call but sometimes i could be five, ten or even more. This example is shown in example one.
Example1:
private String generateId() {
String myId = StaticFunction.generateMyId();
MyMessages doesExist = MyServiceDaoImpl.checkDoesItExistInDB(myId);
if(doesExist != null) {
generateId();
}
return myId;
}
In the second example I suppose that I could create just one call to db and retrieve all items and put them into collection. Then I am able to via stream to search for specific item using also recursive function.
Example2:
private String generateId(List<MyMessages> messages) {
String myId = StaticFunction.generateMyId();
MyMessages myMessage = messages.stream().filter(m ->
m.getCorrelationId.equals(myId)).findFirst().orElse(null);
if (MyMessages != null) {
generateId(messages);
}
return myId;
}
My question is whats is the best approach to make this thing right? Do you have some other solutions? What are the advantages and disadvantages of above examples?

If you cannot use db generated ids, as suggested in the comments, you could use a UUID generator to create the PKs. The probabilities of collision are so low it's not worth checking in the db.
For generating UUIDs in Java take a look at http://docs.oracle.com/javase/7/docs/api/java/util/UUID.html

There's nothing wrong with case 1, DB can do lookups very effeciently when the column is indexed. But - you need to do the DB access.
The second case looks much faster (iterate in memory would be much faster than any DB access), however it has drawbacks: You have to keep all your messages (or at least their correlation ids) in memory and when having A LOT of data, you're scr.. you will have bad time to fix it
As well consider scalability where multiple instances of your application could access a DB.
Therefore I'd suggest to let the database generate the key (you can use e.g. SERIAL data type) and Hibernate returns the generated keys when saving the object. If you need custom ids (generated by your app), you can use uuid where there's low probability of the value conflict
As well you can use UPSERT syntax (INSERT .... ON CONFLICT (correlation_id) ...)
Have fun

Related

I need 2 denominational array sorted [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am working on a project that has a list of student name and numbers, for example
James Bloggs,1
Paul Jonson,43
Andt Peters,23
Once I have them in an array I then need them sorted.
What is the best way of going about this. Its not the sort Im stuck on its the referencing the names to the numbers. I would have thought if I do a 2 denominational array only one would be sorted.
Any help would be great,
EDIT: I just realized this question was asking about a 2-dimensional array and my answer doesn't directly deal with that. I am skeptical that arrays should be involved at all. Arrays are usually for dealing with primitive data, and maybe if you are coming from a C background you'd think they'd be the natural thing to use. If you really honestly have to use arrays then this probably isn't the way to go.
https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
public void foo(){
// Use a TreeMap. It will sort keys on insertion.
Map<Integer,String> nameByNumber = new TreeMap<>();
nameByNumber.put(1, "James Boggs");
// etc. put all the entries in however you need to
List<Integer> sortedNumbers = personByNumber.getKeys();
List<String> namesSortedByNumber = personByNumber.getNames();
}
If you need it to be more organized and complex, you can encapsulate the name and number into a Class with a name and number property. Then you'd still use the number as the key, but you'd have the full class as the value. Do this if you need to have more than just a name, like last name, first name, address, etc.

The cheapest way to compare two sets of strings [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have to sets of strings
set 1:
"hello"
"world"
"stackoverflow"
set 2:
"world"
"hello"
"stackoverflow"
Before I tried to compare the content, I know exactly that these two sets contain only unique values. So I am not thinking about java Set for unique test.
So in Java, what should be the cheapest way to compare these two sets? By cheapest, I mean memory like.
I know I can do ArrayList.contains() forLoop, is there a better way?
And I was told Java HashSet consumes 5 times more resources than ArrayList when containing same length of contents. Is that true?
UPDATE
I don't have sample for you, since this is just an idea came to my mind.
By two sets of strings, I meant literally set, this set can also be stored in Java ArrayList.
what I want
is to compare these two sets of string to know if they are containing the same contents. of course I know before the actions that they both containing unique contents.
UPDATE
Sorry, this is not a practical problem I ran across with. This is just an idea I am wondering about.
I have absolutely no idea about the performance of this, but just to add a possible solution...
String[] a = {"hello","world","stackoverflow"};
String[] b = {"world","hello","stackoverflow"};
Arrays.sort(a);
Arrays.sort(b);
System.out.println(Arrays.equals(a,b) ? "same" : "different");
Result:
same
Since your only concern is memory footprint than basic nested loop on arrays (or whatever collections these strings are stored in at the moment) would work perfectly fine and only use fixed amount of extra space (for loop counters).
You can shorten code by using contains too - that function should require fixed amount of memory, but you'd waste some on call stack frame.
Note that this will trade run-time performance of comparison for memory usage. The penalty for time performance is significant, and likely will be easily noticeable for sets of size more 10-20 items. More balanced approach of comparing the sets is covered for example in What is the fastest way to compare two sets in Java? - memory cost of appropriate data structures is generally less of a concern than run-time cost.

What data type should i use? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
hi i have a java programming assignment wich include 3 exercice i have done 2 already but in the third one i am dont know wich data type i should use
here is the question :
Write a java program that reads from the user the course code, section and the scores of a student for three exams representing two midterms and a final. The percentage of each of the three exams is of fixed value 30, 30 and 40 percent respectively. Your program computes the final average grade and displays it along with the course code and the section.
Remark: All data, except for the average, must be whole numbers and you should use the most efficient data type that is suitable for this specific exercise.
Sample Run:
Enter your course code: CSCI250
Enter your section: E
Enter the scores of the tests and the final: 97 83 77
CSCI250 E 84.8 (result)
so what i want to know is what is the preferable data type to use for course code ? and char is the one that i should use for section right ?
If you're capturing user input, use a String for everything.
Reason? You may request a number, but the user can type anything. Your code needs to handle bad input.
I think you can use String as data type for Course Code. You can write both numbers and letters by using it. And for section, yes, char will be suitable for it.
Use a String for arbitrary text.
If the section code is always present1 and is never more than character than a char can be used.
However, I would still use a String for consistency, flexibility, and easy of use. The teacher may prefer this based on the "efficient"cy they are going for.
1The is a soft "always": while char cannot represent null a sentinel (eg. '\0' or ' ') can be used to indicate 'no section specified'. Using such a sentinel to supplement null can also (but does not always) lead to more logic work - in particular when the record is displayed.
In any case, it is probably best to not switch to Character just for the null as this is most likely outside of the scope of current course work.

Which data type use for column representing Enum? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
In my DB I have a column named type.
In my model package there is an Enum of CREDIT, DEBIT, SAVINGS
My table can have billions of records.
The colum type has to be indexed.
I am using MySQL 5.6
And using JPA for persistency.
My question is: what data type should I use for such column?
I am thinking bout: integer or string.
If my column is int I will use EnumType.ordinal otherwise EnumType.string.
Now which data type is better in case of performance for:
searching based on this key
and to do some join?
Should I use int or string, or maybe there is some better third option?
You should use an INTEGER column if your database does not support enum types directly, since using a string type takes more space and is more difficult computationally to search.
Postgres supports user-defined enums, which the database itself will map into integers internally, and you can use EnumType.STRING with a Postgres backend. This has the advantages of enumerated types, such as easier human reading and range checking. I'm not familiar with what capabilities MySQL has in this regard.
Integers are faster than varchars, and allow sorting in the database using the natural enum ordering. But the readability suffers: 0 is less clear than 'CREDIT' when you look at rows in the database.
If sorting doesn't matter, I would first measure if the performance and space gains are worth the readability loss, and then choose accordingly. Remember that with only 3 different values and such a huge amount of rows, you'd better search on another indexed column first, that divides the amount of rows to look at by much more than 3.

How can I distinguish a String(of characters) leading a minus sign in JAVA? ex: "buy" and "-buy" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am comparatively new in JAVA. I am implementing an idea involving multimap. Now I want to put "buy" and "-buy" as the key. My question is, how can I distinguish that the original string(characters) is same but they have opposing leading sign???
I think you might be misusing the concept of multimaps. I am gathering from your question that you want to look up a single key and have it return values for two keys (with and without '-' before them). Multimaps don't support multiple keys (as far as I'm aware in any case). They support multiple values for each key.
You have a number of options:
Don't encode the 'opposite' semantic in the key. Rather create a new class with the String and a boolean field for flagging opposite and use that class as your key.
public class Operation {
String getName();
Boolean isOpposite();
}
Map> map;
Don't include the logic on opposites in the data structure at all. Rather parse the key on usage. In other words you would need to get both "buy" and "-buy" as keys and then sort out what to do with each in your code.
Make your Map two levels with the second level representing whether the values are opposite or not:
Map<String,Map<Boolean,List<Value>>> map;
map.get("buy").get(true)...
The first option is definitely the best in my view. The text associated with the values should just be one attribute of your key - if you end up having to add others then you will end up with a bunch of logic encoded in the key.
To have a map with multiple values for a single key, where key="buy" and key="-buy", I’d recommend using the MultiValueMap from the Apache Commons Collection library. The Javadoc for the MultiValueMap is here.

Categories

Resources