Which is faster? Looking for data in the bank or reading a cookie?

Asked

Viewed 53 times

0

Guys, in your opinion, what would be better in this situation?

I’m working on an Asp MVC and Entity Framework website. I have a table of federative units where I have (simplifying) 3 columns Id, Nomeestado, Urlestado. Example: 1, Minas Gerais, minasgerais.

There is a state search, where the user selects the state in a listbox and sends the request, containing the State Id for a Search Routing Action.

If this action receives other data for example a city, it will redirect to the action that returns the url with the city Meusite/State/City.

If you receive only the state (the case that interests us here) you will redirect to the action that returns the url with only the state, for example: Meusite/minasgerais

Simple, right?

The (simplified) signature of these actions would be:

public ActionResult Busca(int estado = 0, int cidade = 0 ...)
public ActionResult Estado(string urlEstado)

But notice that I get an id. And when forwarding the action asks for a string with the url. Also because if not the url would be Meusite/1 which is not friendly. Then I search the url in the bank and redirect. Ok.

But then in the action of the state I will need the Id of that state. Then comes my doubt. It would be faster in terms of ranking for the site, fetching the Id in the database by the url (which is also unique). Or read a cookie saved in the Search action containing only id?

I read the cookie like this:

HttpCookie Cookie = Request.Cookies[Nome];
return Server.UrlDecode(Cookie.Values.Get(chaveValoresCookie));

And reading from the bank would be:

int id = ctx.tbEstado.where(e => e.Url == url).select(e => e.Id).FirstOrDefalt();
  • 1

    Reading the cookie would be faster because it comes with the request itself, you don’t need to access another resource

  • Thanks Miguel.I think the question is a bit silly, but since I never worked with cookies, maybe the answer surprised me.

  • gives a look at Node.js it helps a lot in this kind of need, since it is a db json, the cookie can even help you in this situation, but it is not recommended to use cookie for this, cookie is for small things, objects and lists is better put in another place.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.