How much sleep do people in middle and old age really need? Seven hours appears to be the ideal amount of sleep, a team of researchers has found.

Sleep isn't just about taking rest, as it actually plays a critical role in cognitive function and psychological health, the University of Cambridge noted in a news release. One of the ways it aids people's health, for instance, is by removing waste products in the brain.

While people need different amounts of sleep depending on what age they're currently in, sleeping itself tends to be more difficult as people age. In fact, it has been estimated that some 40% to 70% of older adults actually have chronic sleep problems, half of which may even be undiagnosed. This can have various impacts on their cognitive and mental health.

"Sleep duration, psychiatric disorders and dementia are closely interconnected in older adults," the researchers wrote in their study, published Thursday in Nature Aging.

So what is really the optimal amount of sleep for people in the middle-old age group?

A team of researchers from the University of Cambridge and Fudan University in China looked at the U.K. Biobank data of almost 500,000 adults between the ages of 38 and 73. They analyzed their sleeping patterns, mental health and well-being, and even took cognitive tests.

They found that seven hours is the optimal amount of sleep for people in middle and old age. This amount of sleep was good for both their cognitive performance and mental health.

"(B)oth insufficient and excessive sleep duration were significantly associated with a decline in cognition on follow up," the researchers wrote.

In other words, having less or more than seven hours of sleep affected their cognitive performance. Those who slept in longer or shorter durations also showed symptoms of depression and anxiety, and had "worse overall well-being," the university noted.

"For every hour that you moved away from seven hours you got worse," said Barbara Sahakian, one of the study authors and a professor at the University of Cambridge, The Guardian reported. "It's very clear that the processes that go on in our brain during sleep are very important for maintaining our physical and mental health."

According to the researchers, it's possible that disruptions in deep sleep could be behind the association as it is important for memory consolidation. A lack of sleep may also impact the brain's capability to remove toxins.

It is, however, unclear why sleeping for longer hours is detrimental. One possibility is that poor sleep quality would lead people to try to sleep more because they feel tired.

"While we can't say conclusively that too little or too much sleep causes cognitive problems, our analysis looking at individuals over a longer period of time appears to support this idea," one of the study authors, Professor Jianfeng Feng of Fudan University, said. "But the reasons why older people have poorer sleep appear to be complex, influenced by a combination of our genetic makeup and the structure of our brains."

Professor Sahakian also stressed the importance of sleep, especially as people age.

"Finding ways to improve sleep for older people could be crucial to helping them maintain good mental health and well-being and avoiding cognitive decline, particularly for patients with psychiatric disorders and dementias," Sahakian said in the news release.